92

I can do this:

var a = [1,5,7,9,22]
a.count               // 5
a[a.count - 1]        // 22
a[a.endIndex - 1]     // 22

but surely there's a prettier way?

4
  • What's wrong with that way? Presumably, you can provide your own extension to add a ".lastElement" method. Commented Jun 4, 2014 at 5:13
  • 8
    Nothing is wrong with it except that it's ugly. Commented Jun 4, 2014 at 5:20
  • 1
    Maybe not wrong but prone to one-off errors. I'd write an extension as mentioned in the answers. Commented Oct 15, 2014 at 7:59
  • I was hoping Swift has lastIndex var, which available in Kotlin. Seems like I'd have to stick with endIndex - 1 for now. Commented May 18, 2020 at 2:52

6 Answers 6

111

As of beta5 there is now a first and last property

In addition to Array acquiring first and last, the lazy collections also implement first (and last if they can index bidirectionally), as well as isEmpty.

Sign up to request clarification or add additional context in comments.

4 Comments

would be nice to include a link to this documentation
don't tell me, tell @joseph.hainline :)
Would be nice with an example
Thanks for this! Interestingly, the iOS 10 master/detail template in Xcode 8.3.3 still uses code of the form x[x.count-1] -- but MUCH longer -- rather than x.last.
94

Update: Swift 2.0 now includes first:T? and last:T? properties built-in.


When you need to you can make the built-in Swift API's prettier by providing your own extensions, e.g:

extension Array {
    var last: T {
        return self[self.endIndex - 1]
    }
}

This lets you now access the last element on any array with:

[1,5,7,9,22].last

6 Comments

Thanks! BTW, endIndex doesn't need to be referenced via self.
As of Xcode6-Beta5, the last property is built-in (as is first). It returns nil if the array is empty.
The problem with this implementation is that it does crash if the Array is empty.
If you ain't first you're last - Ricky Bobby
you do not need this anymore in Swift 3. See below for better answers. .last is builtin.
|
26

You can fetch the last element of an Array by using the last property.

Code:

let lastElement = yourArray.last

Comments

13

swift 5:

if let last = a.last {
   print(last)
}

Comments

9

You can also get and remove last element from array
removeLast()

Example:

var array = [1, 2, 3]
let removed = array.removeLast()
// array is [1, 2]
// removed is 3

Taken from Apple docs (this source expired but you can find examples in this documentation page)

1 Comment

Unlike the ObjC removeLast feature, Swift 4 isn’t smart enough to know if there are no elements left in the array and will crash if tried on an empty array.
2

edit for swift 2.2

extension Array {
    var last: Element {
        return self[self.endIndex - 1]
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.