Created
June 17, 2015 18:38
-
-
Save SlaunchaMan/369cbab0cc0df8bba321 to your computer and use it in GitHub Desktop.
Getting the Next Element in a Swift 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 { | |
func after(item: T) -> T? { | |
if let index = find(self, item) where index + 1 < count { | |
return self[index + 1] | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A slightly modified version of @mvarie's solution which optionally allows wrapping back to the start or end of the collection: