ios [__NSDictionaryI setObject:forKey:]: 无法识别的选择器发送到实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14731353/
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
[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance
提问by jini
I am trying to add "dateTime" to My dictionary as defined follows:
我正在尝试将“dateTime”添加到我的字典中,定义如下:
Symptom Ranking: {
5111ef19253b4a9150000000 = 1;
5111f029253b4add4e000000 = 1;
5111f036253b4a123d000001 = 1;
5111f045253b4a404f000000 = 1;
}
NSLog(@"date selected: %@", [[self.datePicker date] description])
[self.results setObject:[[self.datePicker date] description] forKey:@"dateTime"];
App crashes and I get this:
应用程序崩溃,我得到了这个:
Symptom Tracker[43134:c07] -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7603990
2013-02-06 08:15:58.741 Symptom Tracker[43134:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7603990'
*** First throw call stack:
(0x171b012 0x1428e7e 0x17a64bd 0x170abbc 0x170a94e 0x521e 0x143c705 0x373920 0x3738b8 0x434671 0x434bcf 0x433d38 0x3a333f 0x3a3552 0x3813aa 0x372cf8 0x2652df9 0x2652ad0 0x1690bf5 0x1690962 0x16c1bb6 0x16c0f44 0x16c0e1b 0x26517e3 0x2651668 0x37065c 0x25dd 0x2505)
回答by
Your dictionary is immutable - it's an NSDictionary
and not an NSMutableDictionary
. Fix that, and it'll work fine.
你的字典是不可变的——它是一个NSDictionary
而不是NSMutableDictionary
. 解决这个问题,它会正常工作。
回答by bugloaf
I ran into this error once when I accidentally declared a copy
property like so:
当我不小心声明了copy
这样的属性时,我遇到了一次这个错误:
@property (nonatomic,copy) NSMutableDictionary* downloadHandlers;
when I did this in my init
:
当我在我的init
:
self.downloadHandlers = [[NSMutableDictionary alloc] init];
I actually got an immutable dictionary. I would have expected that calling copy
on a mutable object would also give me a mutable object, but apparently not. Anyway, removing to the copy
keyword (which I never intended to be there in the first place) fixed the problem.
我实际上得到了一个不可变的字典。我原以为调用copy
可变对象也会给我一个可变对象,但显然不是。无论如何,删除copy
关键字(我从一开始就不想在那里)解决了这个问题。
回答by superarts.org
As the top-ranked answer said you need to use NSMutableDictionary
instead of NSDictionary
. And in case you want to use literals, use mutableCopy
like:
正如排名靠前的答案所说,您需要使用NSMutableDictionary
而不是NSDictionary
. 如果您想使用文字,请使用mutableCopy
类似:
NSMutableDictionary* dict = [@{@"key": @"value"} mutableCopy];
So that you can re-assign keys using
这样您就可以使用重新分配键
dict[@"key"] = @"new-value";
回答by Rich Tolley
You need to be using an NSMutableDictionary
- the stack trace shows you're using an __NSDictionaryI
,(NSDictionary
) which is immutable
您需要使用NSMutableDictionary
- 堆栈跟踪显示您使用的是不可变的__NSDictionaryI
,( NSDictionary
)
回答by Ravi Raja Jangid
This problem occurs most of the time when we are dealing with a web-service response because the data received is immutable. When you try to alter immutable data the application will definitely crash. I hope the following code snippet will help.
当我们处理 Web 服务响应时,这个问题大部分时间都会发生,因为接收到的数据是不可变的。当您尝试更改不可变数据时,应用程序肯定会崩溃。我希望下面的代码片段会有所帮助。
NSMutableDictionary *headerData;
/*Every time you need to allocate memory to the corresponding MutableDictionary variable*/
headerData =[[NSMutableDictionary alloc ]initWithDictionary:response[@"Header"]];
回答by vinodonkar
In My case I had a swift code that returns value in [AnyHashable:AnyObject] format, I had to convert it to NSMutableDictionary,
在我的例子中,我有一个以 [AnyHashable:AnyObject] 格式返回值的快速代码,我必须将它转换为 NSMutableDictionary,
NSMutableDictionary *logDict = [[NSMutableDictionary alloc]initWithDictionary:[[AnalyticsManager shared]addCommonPropertiesWithProperties:logDict]];
NSMutableDictionary *logDict = [[NSMutableDictionary alloc]initWithDictionary:[[AnalyticsManager shared]addCommonPropertiesWithProperties:logDict]];
Where as [[AnalyticsManager shared]addCommonPropertiesWithProperties:logDict] this part returns in [AnyHashable:AnyObject] format. That fixed my issue.
其中 [[AnalyticsManager shared]addCommonPropertiesWithProperties:logDict] 这部分以 [AnyHashable:AnyObject] 格式返回。那解决了我的问题。