How to find index of list item in Swift?

2018-12-31 09:31发布

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 list.EndIndex but I want something like python's list.index("text").

标签: arrays swift
17条回答
何处买醉
2楼-- · 2018-12-31 10:22

Any of this solution works for me

This the solution i have for Swift 4 :

let monday = Day(name: "M")
let tuesday = Day(name: "T")
let friday = Day(name: "F")

let days = [monday, tuesday, friday]

let index = days.index(where: { 
            //important to test with === to be sure it's the same object reference
            $0 === tuesday
        })
查看更多
骚的不知所云
3楼-- · 2018-12-31 10:23

Swift 4.2

func index(of element: Element) -> Int?

var alphabets = ["A", "B", "E", "D"]

Example1

let index = alphabets.index(where: {$0 == "A"})

Example2

if let i = alphabets.index(of: "E") {
    alphabets[i] = "C" // i is the index
}
print(alphabets)
// Prints "["A", "B", "C", "D"]"
查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 10:23

You can also use the functional library Dollar to do an indexOf on an array as such http://www.dollarswift.org/#indexof-indexof

$.indexOf([1, 2, 3, 1, 2, 3], value: 2) 
=> 1
查看更多
不流泪的眼
5楼-- · 2018-12-31 10:24

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
查看更多
倾城一夜雪
6楼-- · 2018-12-31 10:25

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.   
}
查看更多
登录 后发表回答