views:

527

answers:

2

Hello,

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];
+7  A: 

Use NSNumber instead of raw int:

NSString *string = @"foo";
NSNumber *number = [NSNumber numberWithInt:1];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                    string, @"bla1", number, @"bla2",nil];
Aleksejs
thanks that worked!
Flocked
You can only store Objective-C objects in most of the Cocoa collection classes, you can't store primitive types.
Rob Keniger
+3  A: 

In a dictionary you have to store objects, not primary types like int, char etc..

Benj