gpt4 book ai didi

ios - 我可以在 for 循环中将键值对添加到 NSarray 或字典吗?

转载 作者:行者123 更新时间:2023-11-28 22:36:16 25 4
gpt4 key购买 nike

我想从带有键值对的 for 循环中向 NSMutabledictionary 添加值?

目前正在使用这段代码

for(int j=0;j<[array count];j++){
for(int l=0;l<array2 count] ;l++){
// retreive the category from plist ,then check whether its matching with "catid"
NSString *temp=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"cate"][l][@"categories"]];

if([catId isEqualToString:temp]){
// "catid" is matching with temp then ,retreive the corresponding bid from plist
NSString *str=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"bid"]];
// add that value "bid" and key "catid" to a mutabledictionary
NSMutableDictionary *m=[[NSMutableDictionary alloc]init];
[m setValue:str forKey:catId];
}
}
}

结果是每次最终字典“m”的值都被最新值替换,但我希望所有值都在一起?

enter image description here我正在尝试获得这样的输出

最佳答案

我想你可以将字典的初始化移到 for 之外,因为在 for 中它在每个循环中都被初始化,这就是为什么它只有一个值

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
for(int j=0; j < [array count]; j++)
{
NSMutableArray *containerArray = [[NSMutableArray alloc] init];
for(int l=0;l<[array2 count] ;l++)
{
// retreive the category from plist ,then check whether its matching with "catid"
NSString *temp=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"cate"][l][@"categories"]];

if([catId isEqualToString:temp])
{
// "catid" is matching with temp then ,retreive the corresponding bid from plist
NSString *str=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"bid"]];

// add that value "bid" and key "catid" to a mutabledictionary
[containerArray addObject:str];
}
[m setValue:containerArray forKey:catId];
}
}

更新

我已经更新了上面的代码以适应图片中的输出

关于ios - 我可以在 for 循环中将键值对添加到 NSarray 或字典吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894398/

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