Hi guys, I met the following issue when trying to add a new object to nsmutabledictionary. Objective c. po [retails setObject:[NSNumber numberWithInt:self.eventID] forKey:@"event_id"]; error: cannot initialize a parameter of type 'id _Nonnull' with an rvalue of type 'NSString *' passing argument to parameter 'aKey' here appreciated for any help.
NSMutableDictionary addObject for key
Are you sure that self.eventID is an int?
also, use the button up there "<>" to enter code in a post like this:
[retails setObject:[NSNumber numberWithInt:self.eventID] forKey:@"event_id"];
If self.eventID is a string with a value of @"137" try this:
[retails setObject:[NSNumber numberWithInt:[self.eventID intValue]] forKey:@"event_id"];
Thank you for help. But self.eventID is Int. I think the issue is in the forKey section.
It's called debugging. Try something to tell you the problem like the following:
[retails setObject:@"abc" forKey:@"def"];
NSLog(@"this worked"):
[retails setObject:[NSNumber numberWithInt:self.eventID] forKey:@"def"];
NSLog(@"and this worked"):
[retails setObject:[NSNumber numberWithInt:self.eventID] forKey:@"event_id"];
NSLog(@"and even this worked"):
It's a bug in the debugger, kinda. (You should submit a bug report.) You are, basically, running a little code fragment to set the dictionary value, and the key's type is "NSString*", which has the wrong nullability for the "setObject:forKey" method, and — according to current language rules — the nullability can't be overridden via an implicit cast from "NSString*" to the type expected for that parameter ("id<NSCopying>").
The easiest workaround is to cast it yourself:
po [retails setObject:[NSNumber numberWithInt:self.eventID] forKey:(id)@"event_id"]
Note that you can also save yourself a bit of typing by using a "boxed number":
po [retails setObject:@(self.eventID) forKey:(id)@"event_id"]
Very good point. But It still crashed: po [retails setObject:@(self.eventID) forKey:(id)@"event_id"]; error: Execution was interrupted, reason: internal ObjC exception breakpoint(-4).. The process has been returned to the state before expression evaluation.
Hmm, it didn't crash when I tried it in a small test app. It might be crashing for an unrelated reason. Unfortunately, when a NSException is thrown during a debugger expression evaluation, I don't think there's a way of finding out what it is. (There may be debugger commands for this, but I'm not that familiar with them.)
I suggest you try circling around it. Try it with the older NSNumber syntax. Try with a boxed number literal (@3 instead of @(self.eventID) — the parentheses aren't needed when the number itself is a literal). Try:
po [retails objectForKey:(id)@"event_id"]
At worst, you might have to insert test code in your app, so that you can execute the setObject:forKey: at that point and see what the exception is.
(lldb) po retails
{
"item_id" = 43;
qty = 11;
}
(lldb) po [retails setObject:@(self.eventID) forKey:(id)@"event_id"];
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-4)..
The process has been returned to the state before expression evaluation.
(lldb) po [retails objectForKey:(id)@"event_id"]
nil
(lldb)
Thank you for the help. It is odd issue. I will debug more.
bump