Is there a cleaner way to get the last two items of an array in Swift? In general, I try to avoid this approach since it\'s so easy to be off-by-one with the indexes. (Using
I doubt it's going to make you that much happier, but the math is certainly simpler:
func getLastTwo(array: [String]) -> [String] { if array.count <= 1 { return array } else { return array.reverse()[0...1].reverse() } }
Note that reverse() is lazy, so this isn't particularly expensive.
reverse()