What is id in Objective C?
1880
15-Jul-2015
Updated on 12-Sep-2020
Tarun Kumar
15-Jul-2015id can hold pointer of any kind of object.
syntax:
id newPointer = somepointer;
e.g.
It will work perfectly. Here is something you should note that id keyword is not followed by an *, its because id already knows that it is a pointer.
We use this while return objects from the derived class while we overrige the init method as shown below:
-(id)init { if (self = [super init]) { someIVarObject = [SomeClass alloc] init]; } return self; }Here we return id because somebody can initialize an object like this.
BaseClass *base = [[DerivedClass alloc] init];
In this case, we want to make sure we have the right pointer returned from the init method.
Summary: