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.
views:
193answers:
2
+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
2010-05-25 07:47:18
thank you kennyTM your post helped me a lot..
Jaimin
2010-05-25 11:22:22
+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
2010-05-27 18:20:21
And if you then want to get an actual filtered dictionary, you can do `[dictionary dictionaryWithValuesForKeys:filtered]`.
Chuck
2010-05-27 18:25:00