Created
June 12, 2014 14:05
-
-
Save nubbel/0e686e5d78c66d12017d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension Array { | |
var first: T? { | |
if isEmpty { | |
return nil | |
} | |
return self[0] | |
} | |
var last: T? { | |
if isEmpty { | |
return nil | |
} | |
return self[count - 1] | |
} | |
} | |
let array = [1, 2, 3, 4, 5] | |
array.first // => {Some 1} | |
array.last // => {Some 5} | |
let emptyArray: Any[] = [] | |
emptyArray.first // => nil | |
emptyArray.last // => mil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment