Skip to content

Instantly share code, notes, and snippets.

@silvers
Last active May 17, 2018 15:33
Show Gist options
  • Save silvers/e611dc037f42361ee335 to your computer and use it in GitHub Desktop.
Save silvers/e611dc037f42361ee335 to your computer and use it in GitHub Desktop.
swift array extension
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