使用for循环搜索数组中的键

时间:2012-12-25 06:30:42

标签: iphone objective-c ios

我创建了一个字典,并在数组中添加了字典和字典中的名称键值数据,使用for循环来迭代数据。但我想搜索名称为vinod.below的密钥名字是我的代码。

    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");
        }
    }

提前致谢

4 个答案:

答案 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");
    }
}

答案 1 :(得分:0)

好的......或许完整的重写是有序的。你的代码既复杂又简单,在某些地方也是如此,我在下面已经提到过。

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");
    }
}

答案 2 :(得分: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;

    }

}];

答案 3 :(得分: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