2013-03-07 33 views
0

我有3個NSMutableArray小號取對象:_parssedArrayOfName_parssedArrayOfbirthdate_CopyOFSelectedFaceBookFriends如何從NSDictionary中使用ObjectForKey

_parssedArrayOfName有像下面

2013-03-07 13:15:40.003 birthdate reminder 2[1102:15803] asdas{ 
     (
     "Nishith Shah", 
     "Rupal Shah", 
     "Viral Nagori", 
     "Malay Shah", 
     "Heather Joy", 
     "Jatin Patel", 
     "Bhushan H Trivedi", 
     "Umang Patel", 
     "Harshal Arolkar", 
     "Nida Shaikh", 
     "Yuriko Ramirez", 
     "Aysu Can", 
     "Bhargav Khatana", 
     "Rahul Vador", 
     "Viral Dave", 

許多名字在_parssedArrayOfbirthdate有生日像下面

13-03-07 13:15:29.833 birthdate reminder 2[1102:15803] this is what im here(
     (
     "<null>", 
     "07/27", 
     "06/11/1980", 
     "08/22/1990", 
     "<null>", 
     "03/17/1985", 
     "<null>", 
     "10/17/1989", 
     "<null>", 
     "07/20", 
     "12/08", 
     "04/14/1992", 
     "10/16", 
     "<null>", 

and _CopyOFSelectedFaceBookFriends 僅僅是說阿南德Kapadiya

我加入的NSDictionary名稱的所有名稱和出生率的關鍵和出生日期爲價值 ,然後我想用ObjectForKey來從這個字典阿南德kapadiya生日的由用戶選擇的好友列表:

但我得到空值,我的代碼如下plz幫助我

注:生日和名字的許多不同的陣列中是相同的,選擇陣列值始終處於名陣列

注2:可以吧是的這個問題的原因?在選擇器名稱是沒有「」,而在名稱數組中所有名稱都是「」

注意:正如你可以看到我的生日數組包含空值可以是問題嗎?

NSArray *objArr = [[NSArray alloc] initWithArray:_parssedArrayOfbirthdate]; 
NSArray *keyArr =[[NSArray alloc] initWithArray:_parssedArrayOfName]; 
NSArray *selector =[[NSArray alloc] initWithArray:_CopyOFSelectedFaceBookFriends];  NSDictionary *dic = [[[NSDictionary alloc] autorelease] initWithObjects:objArr forKeys:keyArr]; 
NSLog(@"asdas%@",dic.description); 
NSMutableArray *matches = [NSMutableArray array]; 
for (NSString *key in selector) { 
NSLog(@" see it%@",key); 
    NSMutableArray *array1 = [dic objectForKey:key]; 
     NSLog(@" matched%@",array1); 
     [matches addObject:array1]; 
     NSLog(@" matched%@",matches); 

回答

0

你的問題和這段代碼很難理解。不過,我認爲問題在於密鑰字符串並不總是代表dic字典中的有效密鑰。如果您試圖檢查選擇器數組的哪些元素是有效鍵,則需要先使用if語句來檢查它們是否存在。

你的代碼中也有一個autorelease,但所有的內存管理都應該由ARC來處理。

試試這個:

NSArray *birthdates = [[NSArray alloc] initWithArray:_parssedArrayOfbirthdate]; 
NSArray *names =[[NSArray alloc] initWithArray:_parssedArrayOfName]; 
NSArray *fbFriends =[[NSArray alloc] initWithArray:_CopyOFSelectedFaceBookFriends];  
NSMutableArray *matches = [[NSMutableArray alloc] initWithCapacity:1];  

NSDictionary *birthdayDictionary = 
[[NSDictionary alloc] initWithObjects:birthdates forKeys:names]; 

NSLog(@"asdas%@",birthdayDictionary.description); 
NSStrging *birthday; 
for (NSString *friend in fbFriends) { 
    NSLog(@"see it %@",friend); 
    if ([birthdayDictionary objectForKey:friend]) 
    { 
     NSLog(@"matched %@",birthday); 
     [matches addObject:birthday]; 
     NSLog(@"matched %@",matches); 
    } 
} 
+0

非常感謝你的先生回答,我會嘗試這個,現在我會回來這裏,如果問題仍然存在 – 2013-03-07 08:26:40

+0

先生重點做總是存在的,出生日期和名字的數數總是相同 – 2013-03-07 08:30:32

+0

它可以成爲這個問題的原因嗎?在選擇器名稱是與out「」,而在名稱數組中所有名稱都與「」 – 2013-03-07 08:42:39

相關問題