Why aren't objects of type 'id' initialized as pointers with a '*' in Objective-C?

后端 未结 3 2001
醉酒成梦
醉酒成梦 2021-01-25 03:29

If I\'m using Objective-C, here\'s how I declare an initialize an int:

int a = 1;

vs an object:

myObj *a = [[myObj alloc] init]         


        
相关标签:
3条回答
  • 2021-01-25 03:47

    Pavel's answer is correct. Specifically, the "<mysterious-root-type> " is declared in objc.h as:

    typedef struct objc_class *Class;
    typedef struct objc_object {
        Class isa;
    } *id;
    
    0 讨论(0)
  • 2021-01-25 04:01

    Because id is defined as:

    typedef struct objc_object {
        Class isa;
    } *id;
    

    So it's already a pointer.

    0 讨论(0)
  • 2021-01-25 04:08

    Because id means identifier. Identifier, like pointer, identifies the object. Identifier isn't the object itself.

    You can always treat it as typedef <some-mysterious-root-type>* id if you want.

    0 讨论(0)
提交回复
热议问题