Skip to content

Instantly share code, notes, and snippets.

@silvers

silvers/Array+safe.swift

Last active May 17, 2018
Embed
What would you like to do?
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
You can’t perform that action at this time.