Created
June 25, 2015 02:16
-
-
Save beccadax/72f3fcc3274c0e5f12d7 to your computer and use it in GitHub Desktop.
Fully generic safe subscripting in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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