objective-c NSDictionary setValue:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1195744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSDictionary setValue:
提问by kperryua
OK, this is driving me nuts -- please tell me I'm not losing my mind!
好吧,这让我发疯——请告诉我我没有失去理智!
I declare:
我声明:
NSMutableDictionary* generalSettingsDict;
im my .h
我是我的 .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.
我得到“NSMutableDictionary 可能无法响应 setValue:forkey:”并且应用程序在运行时崩溃。
Please help :(
请帮忙 :(
回答by kperryua
A) forkey:should be forKey:. Capitalization is important.
A)forkey:应该是forKey:。大写很重要。
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:
B)setValue:forKey:是一种 KVC 方法。它可能会按您的预期工作,但用于 NSMutableDictionary 的正确方法是setObject:forKey:
回答by Alex
You want setObject:forKey:. setValue:forKey:is part of the key-value coding protocol. NSMutableDictionaryprovides setObject:forKey:.
你要setObject:forKey:。setValue:forKey:是键值编码协议的一部分。NSMutableDictionary提供setObject:forKey:.

