How can I find the index of an item in Swift? -
this question has answer here:
- how find index of list item in swift? 13 answers
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
Post a Comment