Last active
May 17, 2018 15:33
-
-
Save silvers/e611dc037f42361ee335 to your computer and use it in GitHub Desktop.
swift array extension
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
extension Array { | |
subscript (safe index: Int) -> T? { | |
get { | |
return indices(self) ~= index ? self[index] : nil | |
} | |
set (value) { | |
if value == nil { | |
return | |
} | |
if !(indices(self) ~= index) { | |
NSLog("WARN: index:\(index) is out of range, so ignored. (array:\(self))") | |
return | |
} | |
self[index] = value! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment