2014-10-02 47 views
0

過濾對象組我有這個詞典:獲得來自的NSMutableDictionary

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)

回答

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

事情是這樣的:

NSSet* passingKeys = [dict keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) { 
    return [key rangeOfString:@"Phone"].location != NSNotFound; 
    }];