开发者

How to search NSMutableDictionary

开发者 https://www.devze.com 2023-01-19 21:52 出处:网络
I have a simple question. How can I search NSMutable Dictionary? For eg. I have a dictionary like this:

I have a simple question. How can I search NSMutable Dictionary? For eg. I have a dictionary like this:

      A1: Apple
      B1: Banana
      C1:  Cat
      D1:  Dog
      A2: Aeroplane
      B2: Bottle
      A3: Android

Now i want to search all the content (values) whose key starting with letter "A", means I want to search "Apple, Aeroplane开发者_StackOverflow and Android". I know how to search array but not Dictionary. Please help me out.


for (NSString* key in theDictionary) {
   if ([key hasPrefix:@"A"]) {
      // found such a key, do whatever you like e.g.
      [theNewDictionary setObject:[theDictionary objectForKey:key] forKey:key];
   }
}


Well, I don't think there's a built-in method for this.

Try:

NSMutableArray *results = [[[NSMutableArray alloc] init] autorelease];

for (NSString *key in [dictionary allKeys]) {
    if ([[key substringToIndex:1] isEqualToString:@"A"]) {
        [results addObject:[dictionary objectForKey:key]];
    }
}

return [results copy];
0

精彩评论

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

关注公众号