NSObject * obj = [[NSObject alloc] init];
NSDictionary * dict = [NSDictionary dictionaryWithObject:obj forKey:@"test"];
For some reason gives me the following crash when executed :
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable
object'
I can’t pinpoint what the issue is as I’ve done my homework and it seems to be valid code… I’m breaking my head over this!
I just ran your code without any problems. Are you sure it’s not crashing after creating the dictionary? The exception you’re getting seems to indicate that you’re trying to add an object to the dictionary, which can’t be done since NSDictionary is immutable. You need to use NSMutableDictionary if you want to add/remove objects after creation.