Always when I try to set an integer as Object in a NSDictionary the program crashes without a message (nothing in the console). What is wrong in this code? :
NSString *string = @"foo";
int number = 1;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
string, @"bla1", number, @"bla2",nil];
Use NSNumber
instead of raw int
:
Modern Objective-C:
NSString *string = @"foo";
NSNumber *number = @1;
NSDictionary *params = @{@"bla1": string, @"bla2": number};
Old style:
NSString *string = @"foo";
NSNumber *number = [NSNumber numberWithInt:1];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
string, @"bla1", number, @"bla2",nil];
In a dictionary you have to store objects, not primary types like int, char etc..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With