Skip to content

Instantly share code, notes, and snippets.

@bennokress
Last active May 22, 2017 13:16
Show Gist options
  • Save bennokress/ece30a2dabc8bf118b2b01b6454e9f35 to your computer and use it in GitHub Desktop.
Save bennokress/ece30a2dabc8bf118b2b01b6454e9f35 to your computer and use it in GitHub Desktop.
Extensions for Swift's Collection Type
extension Collection {
// Usage: array[optional: 2] --> returns element at position 2 if possible, else returns nil
subscript(optional i: Index) -> Iterator.Element? {
return (self.startIndex ..< self.endIndex).contains(i) ? self[i] : nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment