isa 究竟是什么

Objective-C对象的第一个成员变量,就是isa。

先来看看官方解释,再说一下个人理解。
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.

一个对象(Object)的isa指向这个对象的类(Class),这个对象的类(Class)的isa指向了metaclass。这样,就可以找到相对应的静态方法和变量了。
Objective-C的运行时是动态的,它能让你在运行的时候添加方法或者删除方法以及使用反射。

类的实例对象的 isa 指向它的类;类的 isa 指向该类的 metaclass ;
类的 super_class 指向期父类,如果该类为根类则值为NULL ;
metaclass的 isa 指向根 metaclass ,如果该metaclass是根 metaclass 则指向自身;
metaclass 的 super_class 指向父 metaclass ,如果该 metaclass 是根 metaclass 则指向该 metaclass 对应的类;

// 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;
}

// 打开 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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,776评论 0 9
  • 首先说明,这篇文章几乎都是抄录的别人的博客,简书文章,在此总结,只是为了方便记忆和以后阅读,如果有什么失礼的地方,...
    LiYaoPeng阅读 5,069评论 1 14
  • 翻译参考链接:[objc 解释]:类和元类 英文原文:[objc explain]: Classes and me...
    kakukeme阅读 1,033评论 0 51
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • ✔班级:【周日下午4:00--5:00 学员:孟渲淇。任课教师:李飞】 ✔教学目标:【了解课外知识水稻结的子实为大...
    A越单纯越幸福阅读 278评论 0 0