Skip to content

Instantly share code, notes, and snippets.

@beccadax
Created June 25, 2015 02:16
Show Gist options
  • Save beccadax/72f3fcc3274c0e5f12d7 to your computer and use it in GitHub Desktop.
Save beccadax/72f3fcc3274c0e5f12d7 to your computer and use it in GitHub Desktop.
Fully generic safe subscripting in Swift
extension CollectionType where Index: Comparable {
subscript (safe index: Index) -> Generator.Element? {
guard startIndex <= index && index < endIndex else {
return nil
}
return self[index]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment