Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can int value be assigned to id type + Objective c

Tags:

objective-c

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?

like image 517
tek3 Avatar asked Oct 15 '10 14:10

tek3


People also ask

What is ID type in Objective-C?

id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.

What is Objective-C ID in Swift?

In Swift 3, the id type in Objective-C now maps to the Any type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type.


1 Answers

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];
like image 149
Sven Avatar answered Oct 12 '22 00:10

Sven