0

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

1)构造两个单独的 NSDictionaries 数组(不同的实体) 2)将两个数组与:

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

3)然后归档 combineArrayofDicts :

NSData *dataToSend = [NSKeyedArchiver archivedDataWithRootObject:combinedArrayofDicts];

4) 通过 GameKit 传输

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

5)我将如何管理在接收端遍历这个数组?我想为每个由类名键入的类获取值:

这是通过 NSLog 的外观(2 个 Sizes 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:]: unrecognized selector sent to instance 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);
        }
    }
}
4

3 回答 3

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"]);
    }
}
于 2013-06-25T04:04:05.873 回答
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);
        }
   }
}
于 2013-06-25T04:18:39.887 回答
0

也许您可以尝试:

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

从您的输出看,我不认为“尺寸”是一个字符串。

于 2013-06-25T04:26:06.537 回答