Last active
August 29, 2015 14:19
-
-
Save harlanhaskins/76f7af8056656114ceb1 to your computer and use it in GitHub Desktop.
SafeArray -- a safe Swifty Array abstraction
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 | |
struct SafeArray<T> { | |
private var values: [T] | |
init(_ values: [T]) { | |
self.values = values | |
} | |
subscript(index: Int) -> T? { | |
if index >= 0 && values.count > index { | |
return values[index] | |
} | |
return nil | |
} | |
} | |
extension Array { | |
var safe: SafeArray<T> { | |
return SafeArray(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage