0

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

 [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"]];

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

4

1 回答 1

3

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


您应该阅读最少知识原理和得墨忒耳定律

使这样的结构更直观:

    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"];
于 2012-06-12T16:23:21.507 回答