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?
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With