如何迭代&从NSArray的NSArrays NSArray中检索值

时间:2013-06-25 03:58:58

标签: ios arrays nsarray nsdictionary

我对如何迭代并获取NSDictionaries数组的数组(不同的类/实体)感到困惑。这就是我目前正在做的事情:

1)构建两个独立的NSDictionaries数组(不同的实体) 2)将两个数组组合在一起:

NSMutableArray *combinedArrayofDicts = [[NSMutableArray alloc] initWithObjects: sizesArrayOfDicts, wishListArrayOfDicts , nil];

3)然后归档combinedArrayofDicts:

NSData *dataToSend = [NSKeyedArchiver archivedDataWithRootObject:combinedArrayofDicts];

4)通过GameKit传输

[self.session sendDataToAllPiers:dataToSend withDataMode: GKSendDataReliable error:nil];

5)如何管理在接收端遍历此阵列?我想为每个由classname键入的类获取值:

以下是通过NSLog(2个大小的Dicts和1个Wishlist Dict)看起来的样子

Printing description of receivedArray:
<__NSArrayM 0xbc65eb0>(
<__NSArrayM 0xbc651f0>(
{
    classname = Sizes;
    displayOrder = 0;
    share = 1;
    sizeType = Neck;
    value = "13\" or 33 (cm)";
},
{
    classname = Sizes;
    displayOrder = 0;
    share = 1;
    sizeType = Sleeve;
    value = "34\" or 86 (cm)";
}
)
,
<__NSArrayM 0xbc65e80>(
{
    classname = Wishlist;
    detail = "";
    displayOrder = 0;
    imageString = "";
    latitude = "30.33216666666667";
    link = "http://maps.google.com/maps?q=loc:30.332,-81.41";
    longitude = "-81.40949999999999";
    name = bass;
    share = 1;
    store = "";
}
)
)
(lldb)

在我的for循环中,我发出了这个:

NSString *value = [dict objectForKey:@"classname"];

并获得例外:

  

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayM objectForKey:]:   无法识别的选择器发送到实例0xbc651f0'

对于数组数组中的混合对象类型,这是否令人不悦?

@Will引导我使用正确的结构给出答案..这是最终答案:

NSArray *receivedArray;
if(receivedArray.count>0){

    NSArray *combinedArrayofDicts = [receivedArray objectAtIndex:0];

    if(combinedArrayofDicts.count>=2){

        NSArray *sizesArray = [receivedArray objectAtIndex:0]; // Reference original received array
        for(NSDictionary *sizeDict in sizesArray){
            NSLog(@"%@",sizeDict);
        }

        NSArray *wishListArray = [receivedArray objectAtIndex:1]; // Reference original received array
        for(NSDictionary *wishDict in wishListArray){
            NSLog(@"%@",wishDict);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

如何迭代并获取NSDictionaries数组数组的值

正如您所说,您拥有一系列字典数组,您当前的代码将不会检索类名的值。

您的返回值位于NSArray而不是NSDictionary

所以你需要做类似的事情,

NSString *value = [returnArray[0][0] objectForKey:@"classname"];

你可以迭代并获得像

这样的值
for (int i = 0; i < [returnArray count]; i++) {
    for (int j = 0; j < [returnArray[i] count]; j++) {
        NSDictionary *dict = (NSDictionary*)returnArray[i][j];
        NSLog(@"%@ ...",[dict objectForKey:@"classname"]);
    }
}

答案 1 :(得分:1)

用于获取所需的词典,使用以下代码,
假设从游戏中心接收数组的receivedArray

NSArray *receivedArray;
if(receivedArray.count>0){
    NSArray *combinedArrayofDicts = [receivedArray objectAtIndex:0];
    if(combinedArrayofDicts.count>=2){
        NSArray *sizesArray = [combinedArrayofDicts objectAtIndex:0];
        for(NSDictionary *sizeDict in sizesArray){
            NSLog(@"%@",sizeDict);
        }
        NSArray *wishListArray = [combinedArrayofDicts objectAtIndex:1];
        for(NSDictionary *wishDict in wishListArray){
            NSLog(@"%@",wishDict);
        }
   }
}

答案 2 :(得分:0)

也许你可以试试:

NSString *value = [NSString stringWithFormat:@"%@",[dict objectForKey:@"classname"]];

根据输出的外观,我不认为“Sizes”是一个字符串。