Created
October 23, 2019 19:36
-
-
Save jimmya/33bc78f3c849040cb349a927caf8507c to your computer and use it in GitHub Desktop.
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
import Foundation | |
public extension Array { | |
subscript (safe index: Int) -> Element? { | |
get { | |
return index < count && index >= 0 ? self[index] : nil | |
} | |
set { | |
if let element = newValue, index < count, index >= 0 { | |
self[index] = element | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"0..<count ~= index" is shorter than "index < count && index >= 0"