想給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"];
不知道怎麼正確添加?
你好像把_surveyAnswers1聲明為NSDictionary了:
NSDictionary * _surveyAnswers1;
應該聲明為NSMutableDictionary:
NSMutableDictionary * _surveyAnswers1;
編譯器根據聲明的類型來判斷哪些方法是有效的,NSDictionary
沒有這個方法setObject:forKey:
,因此編譯器會發出警告