I want to add an array to a key in nsdictionary. Such as each key is a question and each questions has multiple answers.
我想在nsdictionary中为一个键添加一个数组。比如每个键都是一个问题,每个问题都有多个答案。
Question1 = [Answer1], [Answer2] , [Answer3];
问题1 = [答案1],[答案2],[答案3];
Question2 = [Answer1], [Answer2] , [Answer3];
问题2 = [答案1],[答案2],[答案3];
With below code I get No Visible @interface for NSDictionary declares the selector 'setObject:forKey:'
使用下面的代码我得到NSVictionary的No Visible @interface声明了选择器'setObject:forKey:'
//answers
_surveyAnswers1 = [[NSMutableDictionary alloc] init];
NSArray *myArrayAnswers1 = [NSArray arrayWithObjects:@"One",@"Two", nil];
NSArray *myArrayAnswers2 = [NSArray arrayWithObjects:@"Three",@"Four", nil];
[_surveyAnswers1 setObject:myArrayAnswers1 forKey:@"FirstKey"];
[_surveyAnswers1 setObject:myArrayAnswers2 forKey:@"SecondKey"];
What is correct way to add an array to a key in NSDictionary?
在NSDictionary中向数组添加数组的正确方法是什么?
2 个解决方案
#1
4
_surveyAnswers1
seems to be declared as an NSDictionary
:
_surveyAnswers1似乎被声明为NSDictionary:
NSDictionary * _surveyAnswers1;
You need to declare it as NSMutableDictionary
:
您需要将其声明为NSMutableDictionary:
NSMutableDictionary * _surveyAnswers1;
The compiler checks what methods are available on the object based on its declared type; NSDictionary
does not have the method setObject:forKey:
, so the compiler warns you (under ARC, this is an error) about that. It would work at runtime, because the object actually is an NSMutableDictionary
, but you should still fix the declaration.
编译器根据其声明的类型检查对象上可用的方法; NSDictionary没有方法setObject:forKey:,因此编译器会警告您(在ARC下,这是一个错误)。它可以在运行时工作,因为该对象实际上是一个NSMutableDictionary,但你仍然应该修复声明。
#2
2
Although you are instantiating _surveyAnswers1 as an NSMutableDictionary, it's declared as an NSDictionary type so either change _surveyAnswers1 to be NSMutableDictionary type or cast _surveyAnswers1 to NSMutableDictionary when you call setObject:forKey:
虽然您将_surveyAnswers1实例化为NSMutableDictionary,但它被声明为NSDictionary类型,因此当您调用setObject:forKey时,将_surveyAnswers1更改为NSMutableDictionary类型或将_surveyAnswers1强制转换为NSMutableDictionary:
[((NSMutableDictionary *)_surveyAnswers1) setObject:myArrayAnswers1 forKey:@"FirstKey"];
[((NSMutableDictionary *)_surveyAnswers1) setObject:myArrayAnswers2 forKey:@"SecondKey"];
#1
4
_surveyAnswers1
seems to be declared as an NSDictionary
:
_surveyAnswers1似乎被声明为NSDictionary:
NSDictionary * _surveyAnswers1;
You need to declare it as NSMutableDictionary
:
您需要将其声明为NSMutableDictionary:
NSMutableDictionary * _surveyAnswers1;
The compiler checks what methods are available on the object based on its declared type; NSDictionary
does not have the method setObject:forKey:
, so the compiler warns you (under ARC, this is an error) about that. It would work at runtime, because the object actually is an NSMutableDictionary
, but you should still fix the declaration.
编译器根据其声明的类型检查对象上可用的方法; NSDictionary没有方法setObject:forKey:,因此编译器会警告您(在ARC下,这是一个错误)。它可以在运行时工作,因为该对象实际上是一个NSMutableDictionary,但你仍然应该修复声明。
#2
2
Although you are instantiating _surveyAnswers1 as an NSMutableDictionary, it's declared as an NSDictionary type so either change _surveyAnswers1 to be NSMutableDictionary type or cast _surveyAnswers1 to NSMutableDictionary when you call setObject:forKey:
虽然您将_surveyAnswers1实例化为NSMutableDictionary,但它被声明为NSDictionary类型,因此当您调用setObject:forKey时,将_surveyAnswers1更改为NSMutableDictionary类型或将_surveyAnswers1强制转换为NSMutableDictionary:
[((NSMutableDictionary *)_surveyAnswers1) setObject:myArrayAnswers1 forKey:@"FirstKey"];
[((NSMutableDictionary *)_surveyAnswers1) setObject:myArrayAnswers2 forKey:@"SecondKey"];