objc isa

objc.h

typedef struct objc_class * Class;

typedef struct objc_object * id;

struct objc_class {
    Class isa ;

    Class super_class                              ;
    long info                                      ; ///< 标识:普通Class/metaClass
    const char * name                              ;
    long version                                   ;
    long instance_size                             ;
    struct objc_ivar_list * ivars                  ;
    struct objc_method_list * * methodLists        ;
    struct objc_cache * cache                      ;
    struct objc_protocol_list * protocols          ;
};

struct objc_object {
    Class isa ;
};

Class 是一个指向 objc_class 结构体的(类)指针。
id 是一个指向 objc_object 结构体(对象)指针。

objc_object 中 isa 指针指向 Class(对象的类),Class 存放对象 ivars 和 methodLists 等。

objc_class 中 isa 指针指向 Class(类的元类),Class 存放静态 ivars 和 methodList 等。

经典使用场景####

1.KVO/KVC isa-swizzling

automatic key-value observing is implemented using a technique called isa-swizzling. This
isa pointer, as the same suggests, point to the object's Class which maintains a dispatch
table essentially contains pointers to the method the Class implements, among other data.
When an observer is register for an attribute of an object the isa pointer of the is modified,
pointing to an intermediate Class rather than at the true Class. As a result the value of the
isa pointer doesn't necessarily reflect the actualy Class of the instance. You should never
rely on the isa pointer to determine Class membership. Instaed, you should use the Class
method to determine the Class of the instance.

isa-swizzling KVO

参考:isa-swizzling
isa-swizzling 实现ReactiveCocoa响应式框架

posted @ 2016-05-05 10:09  lvable  阅读(203)  评论(0)    收藏  举报