如何在SWIFT中找到列表项的索引?我试图通过搜索一个列表来找到一个项目索引。有人知道怎么做吗?我知道有list.StartIndex和list.EndIndex但我想要像蟒蛇那样的东西list.index("text").
3 回答
四季花海
TA贡献1811条经验 获得超5个赞
let index = someArray.firstIndex{$0 === someObject}
class
===
let person1 = Person(name: "John")let person2 = Person(name: "Sue")let person3 = Person(name: "Maria")let person4 = Person(name: "Loner")let people = [person1, person2, person3]let indexOfPerson1 = people.firstIndex{$0 === person1} // 0let indexOfPerson2 = people.firstIndex{$0 === person2} // 1let indexOfPerson3 = people.firstIndex{$0 === person3} // 2let indexOfPerson4 = people.firstIndex{$0 === person4} // nil
let indexOfPerson1 = people.firstIndex(where: {$0 === person1})
index
indexOf
class
Equatable
===
(同一性==
(平等==
people.firstIndex(of: person1)
).
- 3 回答
- 0 关注
- 871 浏览
添加回答
举报
0/150
提交
取消