2 xinyoulingxi1 tong xinyoulingxi1_tong 于 2013.01.11 10:18 提问

给nsdictionary的key添加一个数组

想给nsdictionary的key添加一个数组,其中每个key都是一个question,每个question对应多个answer:


Question1 = [Answer1], [Answer2] , [Answer3];

Question2 = [Answer1], [Answer2] , [Answer3];


然后我尝试用下面的代码时报错:

No Visible @interface for NSDictionary declares the selector '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"];

不知道怎么正确添加?

1个回答

g989_1314125
g989_1314125   2013.01.11 11:09
已采纳

你好像把_surveyAnswers1声明为NSDictionary了:

NSDictionary * _surveyAnswers1;

应该声明为NSMutableDictionary:

NSMutableDictionary * _surveyAnswers1;

编译器根据声明的类型来判断哪些方法是有效的,NSDictionary没有这个方法setObject:forKey:,因此编译器会发出警告

Csdn user default icon
上传中...
上传图片
插入图片