isa 究竟是什么

Objective-C對(duì)象的第一個(gè)成員變量,就是isa。

先來(lái)看看官方解釋,再說(shuō)一下個(gè)人理解。
Every object is connected to the run-time system through itsisa instance variable, inherited from the NSObject class.isa identifies the object's class; it points to a structurethat's compiled from the class definition. Through isa, anobject can find whatever information it needs at run timesuch asits place in the inheritance hierarchy, the size and structure ofits instance variables, and the location of the methodimplementations it can perform in response to messages.

一個(gè)對(duì)象(Object)的isa指向這個(gè)對(duì)象的類(Class),這個(gè)對(duì)象的類(Class)的isa指向了metaclass。這樣,就可以找到相對(duì)應(yīng)的靜態(tài)方法和變量了。
Objective-C的運(yùn)行時(shí)是動(dòng)態(tài)的,它能讓你在運(yùn)行的時(shí)候添加方法或者刪除方法以及使用反射。

類的實(shí)例對(duì)象的 isa 指向它的類;類的 isa 指向該類的 metaclass ;
類的 super_class 指向期父類,如果該類為根類則值為NULL ;
metaclass的 isa 指向根 metaclass ,如果該metaclass是根 metaclass 則指向自身;
metaclass 的 super_class 指向父 metaclass ,如果該 metaclass 是根 metaclass 則指向該 metaclass 對(duì)應(yīng)的類;

// objc.h文件中
#if !OBJC_TYPES_DEFINED
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;

/// Represents an instance of a class.
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

/// A pointer to an instance of a class.
typedef struct objc_object *id;
#endif

// NSObject.h文件中
@interface NSObject <NSObject> {
    Class isa  OBJC_ISA_AVAILABILITY;
}

// 打開(kāi) objc_class 
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;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,074評(píng)論 0 9
  • 首先說(shuō)明,這篇文章幾乎都是抄錄的別人的博客,簡(jiǎn)書(shū)文章,在此總結(jié),只是為了方便記憶和以后閱讀,如果有什么失禮的地方,...
    LiYaoPeng閱讀 5,344評(píng)論 1 14
  • 翻譯參考鏈接:[objc 解釋]:類和元類 英文原文:[objc explain]: Classes and me...
    kakukeme閱讀 1,113評(píng)論 0 51
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,647評(píng)論 19 139
  • ?班級(jí):【周日下午4:00--5:00 學(xué)員:孟渲淇。任課教師:李飛】 ?教學(xué)目標(biāo):【了解課外知識(shí)水稻結(jié)的子實(shí)為大...
    A越單純?cè)叫腋?/span>閱讀 328評(píng)論 0 0

友情鏈接更多精彩內(nèi)容