Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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 从字典数组复制到字典数组_Iphone_Ios8_Nsarray_Nsdictionary - Fatal编程技术网

Iphone 从字典数组复制到字典数组

Iphone 从字典数组复制到字典数组,iphone,ios8,nsarray,nsdictionary,Iphone,Ios8,Nsarray,Nsdictionary,我在一个数组中收集像这样的字典数组中的ID NSArray *places= @[ {place_id = @"3dsfdDfGDH"; place_url = @"/Peru/Lambayeque/Chiclayo"; place_name = @"Peru";} , {place_id = @"HUHiKcZVU7ItMyQ"; place_url = @"/Peru/La+Libertad/Hua

我在一个数组中收集像这样的字典数组中的ID

NSArray *places=   @[ 
    {place_id = @"3dsfdDfGDH";      
    place_url = @"/Peru/Lambayeque/Chiclayo";      
    place_name = @"Peru";}
     ,
    {place_id = @"HUHiKcZVU7ItMyQ";       
    place_url = @"/Peru/La+Libertad/Huanchaco";      
    place_name = @"Peru";}
    ,
    {place_id = @"7JL1K5FVUbg0vg";        
    place_url = @"/United+Kingdom/England/Buckley+Hill";      
    place_name = @"United Kingdom";}
                   ];

NSArray *placeIds= [places valueForKeyPath:place_id]; 
这是完美的工作,但我想要的是另一个数组与地点的id以及地点的url,但不是名称。 差不多

我希望NSArray*PlaceIdAndURL有如下字典

                   @[
         {place_id = @"3dsfdDfGDH";     
        place_url = @"/Peru/Lambayeque/Chiclayo";}
         ,
        {place_id = @"HUHiKcZVU7ItMyQ";       
        place_url = @"/Peru/La+Libertad/Huanchaco";}
        ,
        {place_id = @"7JL1K5FVUbg0vg";        
        place_url = @"/United+Kingdom/England/Buckley+Hill";}
                       ];

我怎样才能在不循环整个数组的情况下获得,就像我得到的第一个数组一样,上面有
ValueForKeyPath
,我认为不可能在一个不迭代的行中实现……但下面是可以完成这项工作的代码。实际上,您可以将此逻辑变成一个方法,并将其作为类别添加到NSArray和NSMutableArray中

places\u dup将包含您正在查找的词典

NSArray *places=   @[
                     @{@"place_id" : @"NFxmX.VVU7LtQwQ",
                       @"place_url" : @"/Peru/Lambayeque/Chiclayo",
                       @"place_name" : @"dadfasdf"}
                     ,
                     @{@"place_id" : @"HUHiKcZVU7ItMyQ",
                       @"place_url" : @"/Peru/La+Libertad/Huanchaco",
                       @"place_name" : @"dadfasdf"}
                     ,
                     @{@"place_id" : @"7JL1K5FVUbg0vg",
                       @"place_url" : @"/United+Kingdom/England/Buckley+Hill",
                       @"place_name" : @"dadfasdf"}
                     ];
NSMutableArray *places_dup = [@[] mutableCopy];
[places enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
    NSDictionary *dictionary = (NSDictionary *) item;
    NSMutableDictionary *filteredDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    [filteredDictionary removeObjectForKey:@"place_name"];
    [places_dup addObject:filteredDictionary];
}];

没有迭代?我怀疑你能。为什么不想迭代呢?请注意,发布的代码不是有效的ObjC代码(尽管我们可能理解其结构,但对于面临相同问题的多个用户来说,它也可能会让人困惑)。我也怀疑它是否可以用一行程序来完成,迭代/创建或某种映射函数可以完成这项工作。因为我还有很多其他键,我只是为了简化而创建了这个键。我正在处理核心数据,所以如果不需要迭代就可以了,否则我就用迭代。如果我用valueForKeyPath方法得到两个单独的数组,我可以用上面的问题中的两个键将它们合并到一个数组中吗?我发布了一个带有工作代码的答案,可能还有另一个建议,但我需要知道您是否关心原始词典是否在过程中被修改,或者是否要创建副本?此块是否会在main Que上运行?这将在当前线程上执行,如果它是main,那么它就是main。