struct objc_class
{
struct objc_class* isa;
struct objc_class* super_class; //root的为null
const char* name;
long version;
long info;
long instance_size;
struct objc_ivar_list* ivars;
struct objc_method_list** methodLists; //方法列表
struct objc_cache* cache; //缓存最近使用的方法,以提高效率;
struct objc_protocol_list* protocols;
};
为什么Class的第一个成员也是Class呢,它的内存布局岂不是和底下的object一样了?其实这就是类对象(class object)与实例对象(instance object)的区别了。
Object-C对类对象与实例对象中的 isa 所指向的类结构作了不同的命名:类对象中的 isa 指向类结构被称作 metaclass,metaclass 存储类的static类成员变量与static类成员方法(+开头的方法);实例对象中的 isa 指向类结构称作 class(普通的),class 结构存储类的普通成员变量与普通成员方法(-开头的方法).
2:对象id:
typedef struct objc_object {
Class isa;
} *id;
可以发现, id可以用来表示任意一个对象,它是一个 objc_object 结构类型的指针,其第一个成员是一个 objc_class 结构类型的指针。
我们的根类NSObject也同样是只有一个Class成员:
@interface NSObject <NSObject> {
Class isa;
}
这个isa到底是什么呢?官方介绍是这样的:
Every object is connected to the run-time system through its isa instance variable, inherited from the NSObject class. isa identifies the object's class; it points to a structure that's compiled from the class definition. Through isa, an object can find whatever information it needs at run timesuch as its place in the inheritance hierarchy, the size and structure of its instance variables, and the location of the method implementations it can perform in response to messages.
可见,一个对象(Object)的isa指向了这个对象的类(Class),而这个对象的类(Class)的isa指向了metaclass。这样我们就可以找到静态方法和变量了。