receiving collection element of type int is not an objective c object in iOS

Atma picture Atma · Dec 7, 2013 · Viewed 20.7k times · Source

I have the following dictionary:

NSDictionary* jsonDict = @{
                               @"firstName": txtFirstName.text,
                               @"lastName": txtLastName.text,
                               @"email": txtEmailAddress.text,
                               @"password": txtPassword.text,
                               @"imageUrl": imageUrl,
                               @"facebookId": [fbId integerValue],
                              };

In the last element, I need to use an integer, but I am receiving the error:

collection element of type int is not an objective c object

How can I use an int value in this element?

Answer

staticVoidMan picture staticVoidMan · Dec 7, 2013

should be:

@"facebookId": [NSNumber numberWithInt:[fbId intValue]];

NSDictionary works with objects only and as a result, we can't store simply ints or integers or bools or anyother primitive datatypes.

[fbId integerValue] returns a primitive integer value (which is not an object)
Hence we need to encapsulate primitive datatypes and make them into objects. which is why we need to use a class like NSNumber to make an object to simply store this crap.

more reading: http://rypress.com/tutorials/objective-c/data-types/nsnumber.html