NSDictionary setValue:

Chris  picture Chris · Jul 28, 2009 · Viewed 33k times · Source

OK, this is driving me nuts -- please tell me I'm not losing my mind!

I declare:

NSMutableDictionary* generalSettingsDict;

im my .h

I init:

generalSettingsDict = [[NSMutableDictionary alloc] initWithCapacity:5];

in a viewWillAppear

I set:

[generalSettingsDict setValue:[NSNumber numberWithBool:control.on]
                       forkey:[NSNumber numberWithInt:control.tag]];

in a method:

-(void)settingChanged:(UISwitch*)control forEvent:(UIEvent *)event

And I get "NSMutableDictionary may not respond to setValue:forkey:" and the app crashes when it is run.

Please help :(

Answer

kperryua picture kperryua · Jul 28, 2009

A) forkey: should be forKey:. Capitalization is important.

B) setValue:forKey: is a KVC method. It may work as you expect here, but the proper method to use for NSMutableDictionary is setObject:forKey: