Last active
January 1, 2021 16:07
-
-
Save yojkim/a8a1bc5237322ce2437c15f8dc9c3ba0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let arr = ["a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa"] | |
func getElementCount(_ index: Int) -> Int { | |
guard let value = arr[safe: index] else { return 0 } | |
return value.count | |
} | |
(-5..<10).forEach { | |
print("\($0) : \(getElementCount($0))") | |
} | |
/* | |
[RESULT] | |
-2 : 0 | |
-1 : 0 | |
0 : 1 | |
1 : 2 | |
2 : 3 | |
3 : 4 | |
4 : 5 | |
5 : 6 | |
6 : 0 | |
7 : 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment