[英][__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance
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)
Your dictionary is immutable - it's an NSDictionary
and not an NSMutableDictionary
. 您的字典是不可变的-它是NSDictionary
而不是NSMutableDictionary
。 Fix that, and it'll work fine. 修复该问题,它将正常工作。
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. 我本来希望在可变对象上调用copy
也会给我一个可变对象,但显然不是。 Anyway, removing to the copy
keyword (which I never intended to be there in the first place) fixed the problem. 无论如何,删除到copy
关键字(我从来没有打算先出现在其中)解决了该问题。
您需要使用NSMutableDictionary
堆栈跟踪显示您正在使用__NSDictionaryI
,( NSDictionary
)是不可变的
As the top-ranked answer said you need to use NSMutableDictionary
instead of NSDictionary
. 正如排名靠前的答案,您需要使用NSMutableDictionary
而不是NSDictionary
。 And in case you want to use literals, use mutableCopy
like: 如果要使用文字,请使用mutableCopy
例如:
NSMutableDictionary* dict = [@{@"key": @"value"} mutableCopy];
So that you can re-assign keys using 这样您就可以使用
dict[@"key"] = @"new-value";
This problem occurs most of the time when we are dealing with a web-service response because the data received is immutable. 当我们处理Web服务响应时,大多数时候都会发生此问题,因为接收到的数据是不可变的。 When you try to alter immutable data the application will definitely crash. 当您尝试更改不可变数据时,应用程序肯定会崩溃。 I hope the following code snippet will help. 希望以下代码段对您有所帮助。
NSMutableDictionary *headerData;
/*Every time you need to allocate memory to the corresponding MutableDictionary variable*/
headerData =[[NSMutableDictionary alloc ]initWithDictionary:response[@"Header"]];
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]];
Where as [[AnalyticsManager shared]addCommonPropertiesWithProperties:logDict] this part returns in [AnyHashable:AnyObject] format. 在[[AnalyticsManager共享] addCommonPropertiesWithProperties:logDict]的情况下,此部分以[AnyHashable:AnyObject]格式返回。 That fixed my issue. 那解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.