Created
May 12, 2016 16:28
-
-
Save benaneesh/143590a695dce3960a9d1c372edb4135 to your computer and use it in GitHub Desktop.
This file contains 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
public extension RangeReplaceableCollectionType where Index: Comparable { | |
public subscript (safe index: Index) -> Generator.Element? { | |
get { | |
guard indices ~= index else { return nil } | |
return self[index] | |
} | |
set { | |
guard indices ~= index else { return } | |
if let newValue = newValue { | |
self.removeAtIndex(index) | |
self.insert(newValue, atIndex: index) | |
} | |
} | |
} | |
public mutating func removeAtIndex(safe index: Self.Index) -> Self.Generator.Element? { | |
guard indices ~= index else { return nil} | |
return self.removeAtIndex(index) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment