NSDictionary setValue:

8 debugging objective-c key-value-observing key-value-coding nsmutabledictionary

好吧,这让我疯了 - 请告诉我,我不会失去理智!

我宣布:

NSMutableDictionary* generalSettingsDict;
Run Code Online (Sandbox Code Playgroud)

我的.h

我初始化:

generalSettingsDict = [[NSMutableDictionary alloc] initWithCapacity:5];
Run Code Online (Sandbox Code Playgroud)

在viewWillAppear中

我设置:

[generalSettingsDict setValue:[NSNumber numberWithBool:control.on]
                       forkey:[NSNumber numberWithInt:control.tag]];
Run Code Online (Sandbox Code Playgroud)

在一个方法中:

-(void)settingChanged:(UISwitch*)control forEvent:(UIEvent *)event
Run Code Online (Sandbox Code Playgroud)

我得到"NSMutableDictionary可能无法响应setValue:forkey:"并且应用程序在运行时崩溃.

请帮忙 :(

kpe*_*yua 25

A)forkey:应该是forKey:.资本化很重要.

B)setValue:forKey:是KVC方法.它可以像你期望的那样工作,但是用于NSMutableDictionary的正确方法是setObject:forKey:


Ale*_*lex 6

你要setObject:forKey:setValue:forKey:是键值编码协议的一部分。NSMutableDictionary提供setObject:forKey:.