NSDictionary setValue:
好吧,这让我抓狂——请告诉我我没有失去理智!
我声明:
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
我得到 “NSMutableDictionary 可能不会响应 setValue:forkey:” 并且应用程序在运行时崩溃。
请帮忙 :(
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 :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
setObject:forKey:
。setValue:forKey:
是键值编码协议的一部分。NSMutableDictionary
提供setObject:forKey:
。You want
setObject:forKey:
.setValue:forKey:
is part of the key-value coding protocol.NSMutableDictionary
providessetObject:forKey:
.A)
forkey:
应该是forKey:
。 大写字母很重要。B)
setValue:forKey:
是一个 KVC 方法。 它可能会像您期望的那样工作,但用于 NSMutableDictionary 的正确方法是setObject:forKey:
A)
forkey:
should beforKey:
. 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 issetObject:forKey: