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;