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];
精彩评论