Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用for循环搜索数组中的键_Iphone_Objective C_Ios - Fatal编程技术网

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

Iphone 使用for循环搜索数组中的键,iphone,objective-c,ios,Iphone,Objective C,Ios,我已经创建了一个dictionary,并在dictionary和dictionary数组中设置名称key-value数据,使用for循环迭代数据。但我想搜索名为vinod的密钥名。下面是我的代码 NSDictionary *dic2 =[[NSDictionary alloc] init]; NSDictionary *dic1 =[[NSDictionary alloc] init]; [dic1 setValue:@"Vinod" forKey:@"f

我已经创建了一个dictionary,并在dictionary和dictionary数组中设置名称key-value数据,使用for循环迭代数据。但我想搜索名为vinod的密钥名。下面是我的代码

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

提前谢谢

好的。。。也许应该进行彻底的重写。您的代码既复杂,又在某些地方完全错误,我在下面已经指出了这一点

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

我希望这将对您有所帮助,这个示例代码使用with block

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;

    }

}];

您的代码包含一些愚蠢的错误,比如向不可变字典添加值

我已更正并找到代码:

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");
    }
}
试试这个:-

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fname = %@",@"Vinod];
    NSMutableArray *aMutArray = [NSMutableArray arrayWithArray:aYourArray];
    [aMutArray filterUsingPredicate:predicate];
    NSMutableDictionary *aDictTemp = [aMutArray objectAtIndex:0];
通常,NSPredicate用于过滤出数组或在数组内部搜索。有关更多信息,请查看此链接:-


您如何在不可变字典中添加对象?它是不可变的,不是吗?您不能向NSDictionary中添加值:D,再次检查第3行和第4行中的错误。您接受的上述答案与此相同,但您不必遍历每个对象。请参考链接。