IOS / Objective-C - struct objc_class * and struct objc_object *

I understand that objects and classes in Objective-C are just structures.

Accordingly, they are fair:

struct objc_class *

and

struct objc_object *

Question number 1:

objc_msgSend(id self, SEL _cmd);

idas far as i know has type struct objc_object *

But when we call a class method, a class that has a type struct objc_class *,

I expect this to cause a problem or scream some warning, such as "Hey, wrong type here, my friend."

But does not exist.

  • Why?

This only serves to satisfy my curiosity, because even without fully understanding this, it does not seem to cause me any problems (so far). But I would like to go deeper and study the basics / "features".

Question number 2:

( № 1), , .

struct objc_class * struct objc_object * ?

:

  • , // ?

  • ( ), ( ) gotchas "(, ( )?)

+4
2

, Objective-C .

, ObjC2 , , . "" (isa). . "struct" .

, ARC -. ( nil), . -> - , . struct , ->. objc_object (isa). , -> , .

, ++ - , . ObjC. .

, , struct objc_class *, , ...

, . objc_object. isa ( , , ). , , . . , . Objective-C - , , . , .

BTW, NSProxy . , , isa ivar. .

struct objc_class * struct objc_object * ?

. , ( ), , .

+7

runtime.h.

struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;


struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

objc_class objc_object isa, isa objc_object , . objc_class , , .

.

.

struct A {
    int a ;
} ;

typedef struct A *PA ;

struct B {
    int a ;
    int b ;
} ;

void dosomething(PA pa)
{
    printf("%d", (*pa).a) ;
}

+ (void)hei
{
    struct B b ;
    b.a = 10 ;
    b.b = 20 ;
    // without forced cast, i will get a warning but work well
    dosomething((PA)&b) ;
}

struct B * struct A *, int a .

, , struct objc_class * objc_msgSend(id self, SEL _cmd);, , .

+5

All Articles