gpt4 book ai didi

ios - 如何从 NSDictionary 的 NSArray 的 NSArray 迭代和检索值

转载 作者:行者123 更新时间:2023-11-28 22:31:33 26 4
gpt4 key购买 nike

我对如何迭代和获取 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) 我将如何管理在接收端遍历这个数组?我想为每个由类名键入的类获取值:

这是它通过 NSLog 的样子(2 个 Sizes Dict 和 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"];

并得到一个异常:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__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);
}
}
}

最佳答案

要获取所需的词典,请使用以下代码,
假设 receivedArray 作为从 Game center 接收的数组

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);
}
}
}

关于ios - 如何从 NSDictionary 的 NSArray 的 NSArray 迭代和检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17288927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com