2012-12-25 29 views
-1

我已經創建了一個字典,並使用for循環迭代數據,將字典和詞典中的名稱鍵值數據設置爲數組。但我想搜索名字是vinod.below的密鑰名是我的代碼。使用for循環搜索數組中的鍵

NSDictionary *dic2 =[[NSDictionary alloc] init];   
    NSDictionary *dic1 =[[NSDictionary alloc] init]; 
    [dic1 setValue:@"Vinod" forKey:@"fname"]; 
    [dic2 setValue:@"vishw" forKey:@"lname"]; 


    [dic2 setValue:@"Tazeen" forKey:@"fname"]; 

    [dic2 setValue:@"momin" forKey:@"lname"]; 


    NSMutableArray *arr =[[NSMutableArray alloc] initWithObjects:dic1,dic2, nil]; 

    NSLog(@"inside the view did load"); 


    NSMutableArray *arr2=[[NSMutableArray alloc] init]; 

    for (int i=0; i<[arr count]; i++) 
    { 
     NSString *str =[arr objectAtIndex:i]; 

     NSLog(@"inside the loop"); 

     [arr2 addObject:str]; 

    // NSArray *arr =[[NSArray alloc] initWithObjects:str, nil]; 
     if ([arr2 containsObject:@"vinod"]) 
     { 
      NSLog(@"first name found in the array"); 
     } 
    } 

在此先感謝

+0

使用NSPredicate .. –

+0

如何通過CA你在不可變的Dictionary中添加對象嗎? –

+0

看到我的回答.... –

回答

3

您的代碼包含一些愚蠢的錯誤,如向不可變詞典添加值。

我已經糾正,並找到代碼:

NSMutableDictionary *dic2 =[[NSMutableDictionary alloc] init]; 
NSMutableDictionary *dic1 =[[NSMutableDictionary alloc] init]; 
[dic1 setValue:@"Vinod" forKey:@"fname"]; 
[dic2 setValue:@"vishw" forKey:@"lname"]; 


[dic2 setValue:@"Tazeen" forKey:@"fname"]; 

[dic2 setValue:@"momin" forKey:@"lname"]; 


NSMutableArray *arr =[[NSMutableArray alloc] initWithObjects:dic1,dic2, nil]; 

for (NSDictionary *dictObj in arr) { 
    if ([[dictObj valueForKey:@"fname"]isEqualToString:@"Vinod"]) { 
     NSLog(@"name found"); 
    } 
} 
0

OK ...也許一個完全重寫是爲了。你的代碼是錯綜複雜的,在一些地方顯然是錯誤的,我在下面提到。

NSMutableDictionary *dic1 =[[NSMutableDictionary alloc] init]; //Your dictionary needs to 
//be mutable, otherwise it will throw exceptions. 
//NSDictionary is immutable. 
NSMutableDictionary *dic2 =[[NSMutableDictionary alloc] init]; 
[dic1 setValue:@"Vinod" forKey:@"fname"]; 
[dic1 setValue:@"vishw" forKey:@"lname"]; 


[dic2 setValue:@"Tazeen" forKey:@"fname"]; 
[dic2 setValue:@"momin" forKey:@"lname"]; 

//Add all the values from the dictionaries into one master array. 
NSMutableArray *arr =[[NSMutableArray alloc] init]; 
[arr addObjectsFromArray:[dic1 allValues]]; 
[arr addObjectsFromArray:[dic2 allValues]]; 

NSLog(@"inside the view did load"); 

//load those values into a set for faster lookup 
NSSet *set = [[NSSet alloc]initWithArray:arr]; 

//For-in the values. Fast enumeration is a lot better at this than c-loops 
for (NSString *value in arr) { 
    if ([set containsObject:@"Vinod"]) { 
     NSLog(@"first name found in the array"); 
    } 
} 
+1

它的不可改變的字典,不是嗎? –

+0

是的,它們是不可變的......哇,這有問題。 – CodaFi

0

我希望這會幫助你,這個例子的代碼使用NSFastEnumeration用塊。

NSDictionary *dic2 =[[NSDictionary alloc] init]; 
NSDictionary *dic1 =[[NSDictionary alloc] init]; 
[dic1 setValue:@"Vinod" forKey:@"fname"]; 
[dic2 setValue:@"vishw" forKey:@"lname"]; 


[dic2 setValue:@"Tazeen" forKey:@"fname"]; 

[dic2 setValue:@"momin" forKey:@"lname"]; 


NSMutableArray *arr =[[NSMutableArray alloc] initWithObjects:dic1,dic2, nil]; 

NSLog(@"inside the view did load"); 

[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 

    if ([[obj valueForKey:@"fname"] isEqual:@"Vinod"]) { 

     NSLog(@"Found it!"); 

     *stop = YES; 

    } 

}]; 
+1

您無法將值添加到NSDictionary:D,第3行和第4行中的錯誤再次檢查。 –

0

「試試這個: -

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fname = %@",@"Vinod]; 
    NSMutableArray *aMutArray = [NSMutableArray arrayWithArray:aYourArray]; 
    [aMutArray filterUsingPredicate:predicate]; 
    NSMutableDictionary *aDictTemp = [aMutArray objectAtIndex:0]; 

通常NSPredicate來篩選出數組或對數組內尋找更​​多的信息檢查。此鏈接: -

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

+0

上面你接受的答案是一樣的,但你不必經過每一個對象請參考鏈接。 – Leena