WHAT IS ID IN OBJECTIVE C?

Tarun Kumar

Total Post:107

Points:535
Posted by  Tarun Kumar
 744  View(s)
Ratings:
Rate this:
  1. Tarun Kumar

    Post:107

    Points:535
    What is id in Objective C?

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!