Skip to content

Instantly share code, notes, and snippets.

@ryanjm
Created May 9, 2015 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanjm/200c7958d75ebac73ae5 to your computer and use it in GitHub Desktop.
Save ryanjm/200c7958d75ebac73ae5 to your computer and use it in GitHub Desktop.
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