tags:

views:

21

answers:

2

Hi all,

I want to know if this assignment is correct

id var = 9;

Will var be assigned the value 9 or do we have to wrap in some wrapper class like NSNumber?

+1  A: 

It is possible, but it is not correct:

warning: initialization makes pointer from integer without a cast

An id is a pointer to an object, not an integer.

  • Do you intend to use the object available at address 9? You will unlikely be allowed to do that.
  • Do you just want to use an id container to carry an integer? You should use NSNumber instead.
  • Do you expect that the assignment will create an object containing 9? This doesn't work like this.
mouviciel
+2  A: 

9 is an integer literal, which is not an object. id is a pointer to an object. If you need to pass an integer as an object (type id) you have to wrap it inside a NSNumber object like this:

id var = [NSNumber numberWithInteger: 9];
Sven