Created
October 17, 2015 15:49
-
-
Save soggybag/f4d3213ddd22e5611a3f to your computer and use it in GitHub Desktop.
Returns element or nil for index in array if there is an element at index
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
extension Array { | |
subscript (safe index: Int) -> Element? { | |
return indices ~= index ? self[index] : nil | |
} | |
} | |
// array[safe: index] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment