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 <
In Swift 2 (with Xcode 7), Array
includes an indexOf method provided by the CollectionType protocol. (Actually, two indexOf
methods—one that uses equality to match an argument, and another that uses a closure.)
Prior to Swift 2, there wasn't a way for generic types like collections to provide methods for the concrete types derived from them (like arrays). So, in Swift 1.x, "index of" is a global function... And it got renamed, too, so in Swift 1.x, that global function is called find
.
It's also possible (but not necessary) to use the indexOfObject
method from NSArray
... or any of the other, more sophisticated search meth dis from Foundation that don't have equivalents in the Swift standard library. Just import Foundation
(or another module that transitively imports Foundation), cast your Array
to NSArray
, and you can use the many search methods on NSArray
.