Skip to content

Instantly share code, notes, and snippets.

@tularovbeslan
Last active May 29, 2019 00:26
Show Gist options
  • Save tularovbeslan/56c2344e1c4a624488d7dab0e88d33cb to your computer and use it in GitHub Desktop.
Save tularovbeslan/56c2344e1c4a624488d7dab0e88d33cb to your computer and use it in GitHub Desktop.
Handling Index Out of Range Exception the Swift Way
extension Collection {
subscript(safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment