开发者

How can i get the keys of NSMutableDictionary that start with a certain string

开发者 https://www.devze.com 2022-12-31 18:06 出处:网络
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

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.


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消