gpt4 book ai didi

ios - 将对象添加到另一个 NSDictionary 中的 NSMutableDictionary 中的 NSMutableArray

转载 作者:行者123 更新时间:2023-11-28 20:34:38 24 4
gpt4 key购买 nike

所以我在字典中有一个包含数组的字典。我正在尝试添加到第二个字典中的数组。

 [mealInfo setObject:[[NSMutableDictionary alloc]init] forKey:@"breakfast"];
[[mealInfo objectForKey:@"breakfast"] setObject:[[NSMutableArray alloc] init] forKey:@"name"];
[[mealInfo objectForKey:@"breakfast"] setObject:[[NSMutableArray alloc] init] forKey:@"detail"];
[[mealInfo objectForKey:@"breakfast"] setObject:[[NSMutableArray alloc] init] forKey:@"ndb"];
[[mealInfo objectForKey:@"breakfast"] setObject:[[NSMutableArray alloc] init] forKey:@"id"];
[[mealInfo objectForKey:@"breakfast"] setObject:[[NSMutableArray alloc] init] forKey:@"alias"];

创建似乎工作只是向数组添加多个元素是我最终遇到 SIGABRT 错误的地方

我这样加

[[[initMealInfo->mealInfo objectForKey:@"breakfast"] objectForKey:@"name"] addObject:[food_prop objectForKey:@"description"]];

我知道我做错了什么,想知道有什么帮助将不胜感激。

最佳答案

首先,您使用的是 ARC 吗?否则你会遇到很多麻烦。


您应该阅读最少知识原则和 Law Of Demeter

正在使这样的结构更直观:

    NSMutableDictionary *mealInfo = [[NSMutableDictionary alloc] init];

NSMutableDictionary *breakfast = [[NSMutableDictionary alloc] init];
[breakfast setObject:[[NSMutableArray alloc] init] forKey:@"name"];
[breakfast setObject:[[NSMutableArray alloc] init] forKey:@"detail"];
[breakfast setObject:[[NSMutableArray alloc] init] forKey:@"ndb"];
[breakfast setObject:[[NSMutableArray alloc] init] forKey:@"id"];
[breakfast setObject:[[NSMutableArray alloc] init] forKey:@"alias"];

[mealInfo setObject:breakfast forKey:@"breakfast"];

然后当您尝试输入信息并将其包装在函数中时,尝试将其分解一下

-(void) addMealName:(NSString*) name forMeal:(NSString*) meal 
{
NSMutableDictionary *meals = [mealInfo objectForKey:meal];
NSMutableArray *mealNames = [breakfast objectForKey:@"name"];
[mealNames addObject:name];
[meals setObject:breakfastNames forKey:@"name"];
[mealInfo breakfast forKey:meal];
}

然后打电话

NSString *newMealName = [food_prop objectForKey:@"description"];
[initMealInfo addMealName:newMealName forMeal:@"breakfast"];

关于ios - 将对象添加到另一个 NSDictionary 中的 NSMutableDictionary 中的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000561/

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