I am trying to find an item index
by searching a list
. Does anybody know how to do that?
I see there is list.StartIndex
and <
For (>= swift 4.0)
It's rather very simple.
Consider the following Array
object.
var names: [String] = ["jack", "rose", "jill"]
In order to obtain the index of the element rose
, all you have to do is:
names.index(of: "rose") // returns 1
Note:
Array.index(of:)
returns an Optional
.
nil
implies that the element isn't present in the array.
You might want to force-unwrap the returned value or use an if-let
to get around the optional.