我对如何迭代和获取 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);
}
}
}