Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SafeArray -- a safe Swifty Array abstraction
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)

This comment has been minimized.

Copy link
Owner Author

@harlanhaskins harlanhaskins commented Apr 20, 2015


let safeNumbers = [1,2,3,4].safe

if let fifth = safeNumbers[4] {
   println("I will never be called")
} else {
   println("I WILL be called")
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.