Skip to content

Instantly share code, notes, and snippets.

@polqf
Created December 4, 2020 17:43
Show Gist options
  • Save polqf/9d789060dcac500d2665649672893d2d to your computer and use it in GitHub Desktop.
Save polqf/9d789060dcac500d2665649672893d2d to your computer and use it in GitHub Desktop.
Returns the element at the specified index if it is within bounds, otherwise nil.
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
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