[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance
Asked Answered
S

6

19

I am trying to add "dateTime" to My dictionary as defined follows:

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)
Synergistic answered 6/2, 2013 at 14:22 Comment(0)
K
35

Your dictionary is immutable - it's an NSDictionary and not an NSMutableDictionary. Fix that, and it'll work fine.

Kathline answered 6/2, 2013 at 14:25 Comment(0)
H
30

I ran into this error once when I accidentally declared a copy property like so:

@property (nonatomic,copy) NSMutableDictionary* downloadHandlers;

when I did this in my 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.

Highwrought answered 12/8, 2014 at 21:38 Comment(1)
I encountered this exact same problem. Thanks!!Bukavu
D
10

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* dict = [@{@"key": @"value"} mutableCopy];

So that you can re-assign keys using

dict[@"key"] = @"new-value";
Delocalize answered 5/9, 2014 at 7:53 Comment(1)
Careful.. literals just end up using [NSDictionary alloc] initWithObjectsAndKeys, which is nil terminated...Byrnes
G
7

You need to be using an NSMutableDictionary - the stack trace shows you're using an __NSDictionaryI,(NSDictionary) which is immutable

Goethe answered 6/2, 2013 at 14:25 Comment(0)
U
7

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.

NSMutableDictionary  *headerData;

/*Every time you need to allocate memory to the corresponding MutableDictionary variable*/

headerData =[[NSMutableDictionary alloc ]initWithDictionary:response[@"Header"]];
Ultramicrometer answered 15/4, 2017 at 13:13 Comment(0)
O
4

In My case I had a swift code that returns value in [AnyHashable:AnyObject] format, I had to convert it to NSMutableDictionary,

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.

Oversubscribe answered 15/12, 2017 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.