我有这本词典:
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:@"1" forKey:@"Name"];
[dic setObject:@"1" forKey:@"Last"];
[dic setObject:@"1" forKey:@"Phone1"];
[dic setObject:@"1" forKey:@"Phone2"];
[dic setObject:@"1" forKey:@"Phone3"];
[dic setObject:@"1" forKey:@"Address"];
仅提取电话号码的最佳方式是什么?
(这是一个动态字典,有时是2个电话号码,有时是5个)
答案 0 :(得分:2)
首先,不要将电话号码设置在不同的键下(因为语言为您提供数组)。
NSMutableDictionary *dic = [NSMutableDictionary new];
NSMutableArray *phoneNumbers = [NSMutableArray new];
dic[@"Name"] = @"1";
dic[@"Last"] = @"1" ;
dic[@"Address"] = @"1";
dic[@"Phone"] = phoneNumbers;
[dic[@"Phone"] addObject:@"123"];
[dic[@"Phone"] addObject:@"213"];
[dic[@"Phone"] addObject:@"456"];
// Now retieve phone numbers
for (NSString *phoneNumber in dic[@"Phone"]) {
NSLog(@"Number: %@", phoneNumber);
}
答案 1 :(得分:1)
这样的事情:
NSSet* passingKeys = [dict keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
return [key rangeOfString:@"Phone"].location != NSNotFound;
}];