Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created October 17, 2015 15:49
Show Gist options
  • Save soggybag/f4d3213ddd22e5611a3f to your computer and use it in GitHub Desktop.
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
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