Created
December 9, 2014 15:06
-
-
Save danielgomezrico/5c3ee686624b994ddd15 to your computer and use it in GitHub Desktop.
Item at index for array in Swift
This file contains 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 { | |
/** | |
Get the index of the item. | |
Array items must conform protocol Equatable. | |
:param: item item used to find it's index | |
:returns: index of the item if founded, | |
otherwise -1 | |
*/ | |
func getIndex<T: Equatable>(item: T) -> Int { | |
for (index, arrayItem) in enumerate(self) { | |
if let arrayItem = arrayItem as? T { | |
if item == arrayItem { | |
return index | |
} | |
} | |
} | |
return -1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment