Created
May 9, 2015 18:26
-
-
Save ryanjm/200c7958d75ebac73ae5 to your computer and use it in GitHub Desktop.
Swift Extension for Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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