views:

193

answers:

2

I have a json response coming and i need to get all the values whose keys are a particular string... for e.g : www_name, www_age etc are coming in the nsmutabledictionary as keys now i want to search all those values having "www_" as their part of the string.

+6  A: 

Loop over the dictionary and filter.

NSMutableArray* result = [NSMutableArray array];
for (NSString* key in dictionary) {
  if ([key hasPrefix:@"www_"]) {
    [result addObject:[dictionary objectForKey:key]];
    // write to a dictionary instead of an array
    // if you want to keep the keys too.
  }
}
return result;
KennyTM
thank you kennyTM your post helped me a lot..
Jaimin
+1  A: 

Rather than iterating over the collection yourself, you could also ask the dictionary to filter the results for you and return the array by using NSPredicate.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith www_"];
NSArray *filtered = [[dictionary allKeys] filteredArrayUsingPredicate:predicate];

Just a thought.

Daniel Johnson
And if you then want to get an actual filtered dictionary, you can do `[dictionary dictionaryWithValuesForKeys:filtered]`.
Chuck
Eveny shinier. Thanks for the tip.
Daniel Johnson