-1

我有一个字典数组,我想获取包含我正在搜索的键的数组的值。

例如我有这个数组叫做locationArray

(
    {
    id = 1,
    info = 
        {
        name = "Person1";
        address = "Chicago";
        };
    };
    id = 2,
    info = 
        {
        name = "Person2";
        address = "Chicago";
        };
    };
    id = 3,
    info = 
        {
        name = "Person3";
        address = "New York";
        };
    };
);

我想获得所有在芝加哥有地址的人的数组。结果 ( chicagoArray) 将是这样的:

(
    {
    id = 1,
    info = 
        {
        name = "Person1";
        address = "Chicago";
        };
    };
    id = 2,
    info = 
        {
        name = "Person2";
        address = "Chicago";
        };
    };
);

我做了这个循环,但我不知道如何处理我的 resultArray:

for (id result in locationArray)
{
     if ([[result valueForKeyPath@"info.address"] isEqualToString:@"Chicago"])
     {
         // What to do inside the resultArray?
         // NSArray *resultArray = [[NSArray alloc] initWithObjects:@""];
     }
}

我怎样才能做到这一点?谢谢!

4

2 回答 2

3
NSMutableArray* entries= [NSMutableArray array];
for(NSDictionary* dict in array)
{
    NSDictionary* innerDict= dict[@"info"];
    if([innerDict[@"address"] isEqualToString: @"Chicago"])
    {
        [entries addObject: dict];
    }
}
于 2013-06-22T13:57:35.320 回答
1

您的数组包含一个NSDictionary.

您可以遍历并检查条件,如果满足则将该对象添加到新数组中。

于 2013-06-22T13:54:27.103 回答