Manju Basha
Manju Basha

Reputation: 665

Search for a value in Array of Dictionaries of array

Here is my response format :

[
  {
    "id": 9001,
    "name": "A to B",
    "description": "A to B via mooncity",
    "sequence":[
      1001,
      1002,
      1003
      ]
  },
 {
    "id": 9002,
    "name": "B to D",
    "description": "B to D via suncity",
    "sequence":[
      1010,
      1002,
      1009
      ]
  }
]

Now i want to get all dictionaries which has 1002 in their sequence array and put it into another array.

I tired

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"%K contains[c] %@", @"sequence", @"1002"];

But the above predicate works well for array of dictionaries. Whereas it fails to work in this case.

Any help will be greatly appreciated.

Upvotes: 0

Views: 464

Answers (2)

Harish
Harish

Reputation: 21

 NSPredicate *pred = [NSPredicate predicateWithFormat:@"CAST(sequence, 'NSString') contains[cd] %@", @"1002"];
 NSArray *result = [your_array filteredArrayUsingPredicate:pred];

Upvotes: 2

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

in swift it will works for me

let array = ["101","110","133"]
    let dict = ["sequence":array,"id": "10"] as [String : AnyObject]

    let array1 = ["100","110","133"]
    let dict1 = ["sequence":array1,"id": "10"] as [String : AnyObject]

    let fArray : NSArray = [dict,dict1] as NSArray


    let pred = NSPredicate(format:  "ANY self.sequence contains[c] %@", "100")

    let test = fArray.filteredArrayUsingPredicate(pred)

    print(test)

out put

[{
id = 10;
sequence =     (
    100,
    110,
    133
);

}]

Upvotes: 1

Related Questions