WHAT IS ID IN OBJECTIVE C?

Tarun Kumar

Total Post:107

Points:535
Posted by  Tarun Kumar
 945  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.  id is a reserved keyword.
    2.  It can hold pointer to any object.
    3.  It can hold pointer to nil.

      Modified On Mar-28-2018 04:39:50 AM

Answer

NEWSLETTER

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