Irrd Irrd - 4 months ago 14
Swift Question

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

I am searching a value of a key in

Dictionary Array
in this way.

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


But wat I want to do is take the index of this
result
contains object.

How can I do that?

Answer

You can enumerate array:

if let indexAndResultTuple = (self.lasrIDArray.enumerate().flatMap{($0.index, $0.element["\(self.selectedTitle)"])}).first {
    let index = indexAndResultTuple.0
    let result = indexAndResultTuple.1

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