Skip to content

Instantly share code, notes, and snippets.

@yojkim
Last active January 1, 2021 16:07
Show Gist options
  • Save yojkim/a8a1bc5237322ce2437c15f8dc9c3ba0 to your computer and use it in GitHub Desktop.
Save yojkim/a8a1bc5237322ce2437c15f8dc9c3ba0 to your computer and use it in GitHub Desktop.
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