Home > DeveloperSection > Interviews > What is id in Objective C?

Posted on    July-15-2015 12:37 AM

 iPhone iPhone  Ios  Objective C 
Ratings:
 1 Answer(s)
  463  View(s)
Rate this:

Tarun Kumar

Total Post:107

Points:535
Posted on    July-15-2015 12:37 AM

id can hold pointer of any kind of object. 
syntax:

id newPointer = somepointer;
e.g.
NSString *str = @"karm";
id newStr = str;


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:

  1. 1. id is a reserved keyword.
  2. 2. It can hold pointer to any object.
  3. 3. It can hold pointer to nil.

Modified On Jul-15-2015 08:43:28 AM

Don't want to miss updates? Please click the below button!

Follow MindStick