Skip to content

Instantly share code, notes, and snippets.

@nurtugan
Created March 11, 2020 09:53
Show Gist options
  • Save nurtugan/95385eab23e3c79027cc4068e12b3c3e to your computer and use it in GitHub Desktop.
Save nurtugan/95385eab23e3c79027cc4068e12b3c3e to your computer and use it in GitHub Desktop.
Safe subscripting of Collection
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment