Objective c 从添加到NSDictionary中获取错误
我初始化了NSDictionary、main_dict和NSMustable数组insert。然后我将主_事件添加到插入数组中。最后,使用键值将该数组添加到字典中 当我进入Objective c 从添加到NSDictionary中获取错误,objective-c,ios,cocoa,Objective C,Ios,Cocoa,我初始化了NSDictionary、main_dict和NSMustable数组insert。然后我将主_事件添加到插入数组中。最后,使用键值将该数组添加到字典中 当我进入[main_dict setValue:insert forKey:count]时,我得到了SIG_ABRT信号 怎么了?我已经初始化了所有元素并有了值,但仍然是SIG_ABRT 无法修改NSDictionary类型实例。替换 main_dict = [[NSDictionary alloc] init]; NSMutable
[main_dict setValue:insert forKey:count]时,我得到了SIG_ABRT信号代码>
怎么了?我已经初始化了所有元素并有了值,但仍然是SIG_ABRT 无法修改NSDictionary类型实例。替换
main_dict = [[NSDictionary alloc] init];
NSMutableArray *insert = [[NSMutableArray alloc] init];
[insert addObject:main_event];
NSString *count = [NSString stringWithFormat:@"%i",total_count];
[main_dict setValue:insert forKey:count];
到
将解决此问题。您无法修改NSDictionary类型实例。替换
main_dict = [[NSDictionary alloc] init];
NSMutableArray *insert = [[NSMutableArray alloc] init];
[insert addObject:main_event];
NSString *count = [NSString stringWithFormat:@"%i",total_count];
[main_dict setValue:insert forKey:count];
到
将解决此问题。您需要将main\u dict
的数据类型更改为可变类型NSDictionary
是不可变的类型,您无法编辑它。因此,将其更改为main_dict=[[NSMutableDictionary alloc]init]代码>您需要将main\u dict
的数据类型更改为可变类型NSDictionary
是不可变的类型,您无法编辑它。因此,将其更改为main_dict=[[NSMutableDictionary alloc]init]代码>您还需要将main_dict的类型从NSDictionary*更改为NSMutableDictionary*否则在尝试调用setValue时会出现编译器错误。事实上,应该已经有这样的错误或警告了。永远不要忽略编译器警告。所有代码都应该编译干净。您还需要将main_dict的类型从NSDictionary*更改为NSMutableDictionary*,否则在尝试调用setValue时会出现编译器错误。事实上,应该已经有这样的错误或警告了。永远不要忽略编译器警告。所有代码都应该编译干净。有几件事:使用特定的对象构造函数:count=。。。;insert=[NSArray arrayWithObject:main_事件];main_dict=[NSDictionary Dictionary WithObject:insert-forKey:count]代码>;如果使用新语法(XCode 4.5+):count=…,会更容易些。。。;main_dict=@{count:@[main_event]}
。如果您可以将count
用作NSNumber
而不是字符串,则更容易:main\u dict=@{(count):@[main\u event]}一些事情:使用特定的对象构造函数:count=。。。;insert=[NSArray arrayWithObject:main_事件];main_dict=[NSDictionary Dictionary WithObject:insert-forKey:count]代码>;如果使用新语法(XCode 4.5+):count=…,会更容易些。。。;main_dict=@{count:@[main_event]}
。如果您可以将count
用作NSNumber
而不是字符串,则更容易:main\u dict=@{(count):@[main\u event]}代码>。
main_dict = [[NSMutableDictionary alloc] init];