caipivara
12/9/2014 - 3:06 PM

Item at index for array in Swift

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
		
	}
	
}