I want to add an array to a key in nsdictionary. Such as each key is a question and each questions has multiple answers.
Question1 = [Answer1], [Answer2] , [Answer3];
Question2 = [Answer1], [Answer2] , [Answer3];
With below code I get 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"];
What is correct way to add an array to a key in NSDictionary?
_surveyAnswers1seems to be declared as anNSDictionary:You need to declare it as
NSMutableDictionary:The compiler checks what methods are available on the object based on its declared type;
NSDictionarydoes not have the methodsetObject:forKey:, so the compiler warns you (under ARC, this is an error) about that. It would work at runtime, because the object actually is anNSMutableDictionary, but you should still fix the declaration.