Swift's array convenience properties

Aug 6, 2014 00:00 · 73 words · 1 minute read swift

One thing one often finds themselves needing to do to an array is access the first and last elements. Of course, this can be done by referencing [0] for the first, or using count for the last, but those are both a bit inelegant.

Now with Xcode 6 Beta 5 we have two new convenience properties, first & last.

var africanAnimals = ["Lion",
                      "Thomson's Gazelle",
                      "Elephant",
                      "Zebra"]

africanAnimals.first // Lion
africanAnimals.last  // Zebra