How can I find the index of an item in Swift? -


this question has answer here:

is there method called indexof or similar?

var array = ["jason", "charles", "david"]  indexof(array, "jason") // should return 0 

edit: of swift 3.0, should use .index(where:) method instead , follow change in swift 2.0 edit below.

edit: of swift 2.0, should use indexof method instead. returns nil or first index of argument.

if let = array.indexof("jason") {     print("jason @ index \(i)") } else {     print("jason isn't in array") } 

use find function. returns either nil (if value isn't found) or first index of value in array.

if let = find(array, "jason") {     println("jason @ index \(i)") } else {     println("jason isn't in array") } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -