swift 如何在字典中获取特定对象的索引

How to get the index of a specific object in Dictionary in swift

我正在以这种方式在 Dictionary Array 中搜索一个键的值。

if let result = self.lasrIDArray.flatMap({[=11=]["\(self.selectedTitle)"]}).first  {
            print("-------RESULT------\(result)") //->title of the other
}

但是我想做的是获取这个 result 包含对象的索引。

我该怎么做?

尝试

self.lasrIDArray 中的项目 {

if self.lasrIDArray.containsObject(item.valueForKey("yourKey")!){

//你的逻辑在这里

} }

你可以枚举数组:

if let indexAndResultTuple = (self.lasrIDArray.enumerate().filter{[=10=].element["\(self.selectedTitle)"] != nil}).first {
    let index = indexAndResultTuple.0
    let result = indexAndResultTuple.1["\(self.selectedTitle)"]!

    print("-------RESULT------\(result) --atIndex--\(index)") //->title of the other
}