Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift Extension for Array
extension Array {
// Underscore is to opt out of Swift forcing an external name for
// the default property.
func lastObjects(_ c:Int = 1) -> Array? {
if (c == 0) {
return Array()
}
else if (c < 0) {
return nil
}
var length = self.count
var lastIndex = length - 1
var startIndex = length - c
return Array(self[startIndex...lastIndex])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.