Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 250
Atma Avatar asked Dec 07 '13 19:12

Atma


Video Answer


2 Answers

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

like image 103
staticVoidMan Avatar answered Sep 21 '22 16:09

staticVoidMan


assuming fbID is an int then It should be like:

@"facebookId": @(fbId)
like image 43
Himanshu padia Avatar answered Sep 21 '22 16:09

Himanshu padia