Skip to content

Instantly share code, notes, and snippets.

@danielgomezrico
Created December 9, 2014 15:06
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 danielgomezrico/5c3ee686624b994ddd15 to your computer and use it in GitHub Desktop.
Save danielgomezrico/5c3ee686624b994ddd15 to your computer and use it in GitHub Desktop.
Item at index for array in Swift
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