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 custom class, you need to implement the Equatable protocol.
import Foundation
func ==(l: MyClass, r: MyClass) -> Bool {
return l.id == r.id
}
class MyClass: Equtable {
init(id: String) {
self.msgID = id
}
let msgID: String
}
let item = MyClass(3)
let itemList = [MyClass(1), MyClass(2), item]
let idx = itemList.indexOf(item)
printl(idx)
In Swift 4/5, use "firstIndex" for find index.
let index = array.firstIndex{$0 == value}
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<Int>
.
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.
As swift is in some regards more functional than object-oriented (and Arrays are structs, not objects), use the function "find" to operate on the array, which returns an optional value, so be prepared to handle a nil value:
let arr:Array = ["a","b","c"]
find(arr, "c")! // 2
find(arr, "d") // nil
Update for Swift 2.0:
The old find
function is not supported any more with Swift 2.0!
With Swift 2.0, Array
gains the ability to find the index of an element using a function defined in an extension of CollectionType
(which Array
implements):
let arr = ["a","b","c"]
let indexOfA = arr.indexOf("a") // 0
let indexOfB = arr.indexOf("b") // 1
let indexOfD = arr.indexOf("d") // nil
Additionally, finding the first element in an array fulfilling a predicate is supported by another extension of CollectionType
:
let arr2 = [1,2,3,4,5,6,7,8,9,10]
let indexOfFirstGreaterThanFive = arr2.indexOf({$0 > 5}) // 5
let indexOfFirstGreaterThanOneHundred = arr2.indexOf({$0 > 100}) // nil
Note that these two functions return optional values, as find
did before.
Update for Swift 3.0:
Note the syntax of indexOf has changed. For items conforming to Equatable
you can use:
let indexOfA = arr.index(of: "a")
A detailed documentation of the method can be found at https://developer.apple.com/reference/swift/array/1689674-index
For array items that don't conform to Equatable
you'll need to use index(where:)
:
let index = cells.index(where: { (item) -> Bool in
item.foo == 42 // test if this is the item you're looking for
})
Update for Swift 4.2:
With Swift 4.2, index
is no longer used but is separated into firstIndex
and lastIndex
for better clarification. So depending on whether you are looking for the first or last index of the item:
let arr = ["a","b","c","a"]
let indexOfA = arr.firstIndex(of: "a") // 0
let indexOfB = arr.lastIndex(of: "a") // 3
Update for Swift 2:
sequence.contains(element): Returns true if a given sequence (such as an array) contains the specified element.
Swift 1:
If you're looking just to check if an element is contained inside an array, that is, just get a boolean indicator, use contains(sequence, element)
instead of find(array, element)
:
contains(sequence, element): Returns true if a given sequence (such as an array) contains the specified element.
See example below:
var languages = ["Swift", "Objective-C"]
contains(languages, "Swift") == true
contains(languages, "Java") == false
contains([29, 85, 42, 96, 75], 42) == true
if (contains(languages, "Swift")) {
// Use contains in these cases, instead of find.
}
Just use firstIndex method.
array.firstIndex(where: { $0 == searchedItem })