Find element index in array by value

 Problem

I have an Array and want to get the index of the first occurrence (if any) of a specific element.



 Recipe


// Initialize the Array
let arr = ["one", "two", "three", "four"]
 
// Find first occurence of "two" (if any)
var location = arr.indexOf("two")

print(location!)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground