How to Find the Index of an Item in Swift

How to find index of list item in Swift?

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

Use firstIndex and lastIndex - 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

Find index of objects array

index(of: )

gets the Person in your case - it is generic function.

index(where: ) 

gets the condition for which you want to find particular Person

What you could do:

personsArray.index(where: { $0.name == "person1" })

Or you could send object to:

personsArray.index(of: existingPerson)

For both options you could get nil - you will have to check it for nil (or guard it).

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

EDIT: As of Swift 3.0, you should use the .index(where:) method instead and follow the change in the Swift 2.0 edit below.

EDIT: As of Swift 2.0, you should use the indexOf method instead. It too returns nil or the first index of its argument.

if let i = array.indexOf("Jason") {
print("Jason is at index \(i)")
} else {
print("Jason isn't in the array")
}

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

if let i = find(array, "Jason") {
println("Jason is at index \(i)")
} else {
println("Jason isn't in the array")
}

Swift - How to find the index of an item that is in an array within an array

It looks like what you want is:

var locationBottom = card_array[0].index(of: "4_of_spades")
var locationTop = card_array[0].index(of: "king_of_diamonds2")

Instead, you're looking for an instance of an array containing solely one card in an array that contains an array of all 52 cards.

Swift - How to get indexes of filtered items of array

You can create your own extension for arrays.

extension Array where Element: Equatable {
func indexes(of element: Element) -> [Int] {
return self.enumerated().filter({ element == $0.element }).map({ $0.offset })
}
}

You can simply call it like this

items.indexes(of: "A") // [0, 2, 4]
items.indexes(of: "B") // [1]

Get index of item in dictionary by key in Swift

If I understand you correctly, you could do it like so:

let myList = [
2: "Hello",
4: "Goodbye",
8: "Whats up",
16: "Hey"
]

let index = Array(myList.keys).index(of: property.propertyValue)

And then to find the key you're looking for again...

let key = Array(myList.keys)[index!]

As said in other answers, a dictionary is probably not the data structure you're looking for. But this should answer the question you've asked.

How can I find min or max valued item index for one of struct array items with Swift 5

You can use max(by:) and pass in a closure that returns whether one index of the array is "smaller" than another. The method will use this to determine the index that is not smaller than any other index.

let index = myStruct.indices.max(by: 
{ myStruct[$0].item1 < myStruct[$1].item1 }
)


Related Topics



Leave a reply



Submit