Skip to content

Instantly share code, notes, and snippets.

@jeffery812
Last active February 29, 2020 10:20
Show Gist options
  • Save jeffery812/e451e4bc73b1d768cbd3e5667258d76c to your computer and use it in GitHub Desktop.
Save jeffery812/e451e4bc73b1d768cbd3e5667258d76c to your computer and use it in GitHub Desktop.
Returns the element at the specified index if it is within bounds, otherwise nil.
public 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