0

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?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Irrd
  • 325
  • 1
  • 6
  • 18
  • Possible duplicate of [How to find index of list item in Swift?](http://stackoverflow.com/questions/24028860/how-to-find-index-of-list-item-in-swift) – l'L'l Jul 21 '16 at 08:32
  • 1
    For dictionaries http://stackoverflow.com/questions/24640990/how-do-i-get-the-key-at-a-specific-index-from-a-dictionary-in-swift – l'L'l Jul 21 '16 at 08:34

2 Answers2

1

You can enumerate array:

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

    print("-------RESULT------\(result) --atIndex--\(index)") //->title of the other
}
Yury
  • 6,044
  • 3
  • 19
  • 41
  • 1
    @dfri if I understood question, there is array of dictionaries, and we need index of first dictionary that contains value for key "\(self.selectedTitle)" – Yury Jul 21 '16 at 08:52
  • 1
    On a closer look on the somewhat sparsely described question, I believe you're right, my bad! – dfrib Jul 21 '16 at 08:56
  • Thanks for the answer. Can you tell me what is the index at $0.index – Irrd Jul 21 '16 at 11:34
  • I am getting a syntax error near the comma at $0.index, it asking me to add ; – Irrd Jul 21 '16 at 11:35
  • @Irrd looks like syntax bug. Check my answer edited changes – Yury Jul 21 '16 at 11:39
  • I got the result . when I print the indexAndResultTuple it shows as 0,nil but that key is not the 0th index. but im always getting this result. Why is that? – Irrd Jul 21 '16 at 13:42
  • @Irrd did you try last version of my code? indexAndResultTuple can not have nil as second value – Yury Jul 21 '16 at 13:44
0

try

for item in self.lasrIDArray {

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

//your logic here

} }

S_S
  • 9
  • 3