Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nubbel/0e686e5d78c66d12017d to your computer and use it in GitHub Desktop.
Save nubbel/0e686e5d78c66d12017d to your computer and use it in GitHub Desktop.
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