The essence of objects/classes/metaclasses in Objective-C

The essence of objects/classes/metaclasses in Objective-C

1. Class

1. The essence of Class

In objc/runtime.h, the Class(s) is defined as the point objc_classpointer structure, objc_classthe data structure of the following structure:

typedef struct objc_class *Class;

struct objc_class {
    Class _Nonnull isa;                                          //objc_class  
#if !__OBJC2__
    Class _Nullable super_class;                                 // 
    const char * _Nonnull name;                                  // 
    long version;                                                //  0
    long info;                                                   // 
    long instance_size;                                          // ;
    struct objc_ivar_list * _Nullable ivars;                     // 
    struct objc_method_list * _Nullable * _Nullable methodLists; // 
    struct objc_cache * _Nonnull cache;                          // 
    struct objc_protocol_list * _Nullable protocols;             // 

objc_classThe first structure is a member variable isa , isa save the objc_classpointer to an instance of the structure, in other words, Class(s) is the essence of an object, called the class object .

2. Method

objc_classStructure struct objc_method_list **methodLists(method list) The structure is used to store the list of methods

struct objc_method_list {
    struct objc_method_list * _Nullable obsolete;
    int method_count;
#ifdef __LP64__
    int space;
    /* variable length structure */
    struct objc_method method_list[1];

In objc/runtime.hthe, Method(method) is defined as the point objc_methodpointer structure objc_methodstructure data structures:

typedef struct objc_method *Method;

struct objc_method {
    SEL _Nonnull method_name;                    // 
    char * _Nullable method_types;               // 
    IMP _Nonnull method_imp;                     // 

objc_methodThe structure contains method_name(method name), method_types(method type: method parameter type and return value type) and method_imp(method implementation). MethodThe SEL(method declaration) and IMP(method) the associated (key-value pairs: SEL: IMP), when sending a message to an object, will be given by SEL(method declaration) to find IMP(implemented method), and then executed.

1. SEL (Method Statement)

In objc/objc.h, the SEL(method declaration) is defined as the point objc_selectorstructure pointer.

typedef struct objc_selector *SEL;

runtimeNo clear definition was found in the relevant header files. Through the test: it SELis just a string that saves the method name. Objective-C will generate a unique integer identifier (Int type address) based on the name and parameter sequence of each method during compilation. This identifier is SEL.

2. IMP (method realization)

In objc/objc.hthe, IMP(implemented method) is defined as a pointer to a function.

typedef void (*IMP)(void /* id, SEL, ... */ ); 
typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...); 

IMPThe essence of is a function pointer, and what it points to is the realization of the method. IMPUsed to find the address of the function, and then execute the function.

IMPThe first function is a function of a parameter points pointing selfpointer (if it is an instance method, the memory address of the class; if that is a class method, the pointer is pointing metaclass), the second parameter is a SELtype of method selector , Next is the actual parameter list of the method, and returns an id. Through a set idand SELcan be determined only way to implement address parameter. In other words, IMP is the execution code that is finally called by the message, and it is the real implementation code of the method.

3. method_types (method type)

method_types Is a string used to store the parameter type and return value type of the method.

3. Ivar (instance variable)

objc_classIn the structure struct objc_ivar_list *ivars(instance variable list), the structure is used to store the list of member variables.

struct objc_ivar_list {
    int ivar_count;
#ifdef __LP64__
    int space;
    /* variable length structure */
    struct objc_ivar ivar_list[1];

In objc/runtime.h, the Ivar(instance variables) is defined as the point objc_ivarstructure pointer to objc_ivarthe structure data structures:

typedef struct objc_ivar *Ivar;

struct objc_ivar {
    char * _Nullable ivar_name;	//  
    char * _Nullable ivar_type;	//  
    int ivar_offset;						//
#ifdef __LP64__
    int space;
4. Cache (Method Cache)

objc_classStructure struct objc_cache *cache(method cache). In objc/runtime.h, the Cache(Method buffer) is defined as the point objc_cachestructure pointer to objc_cachethe structure data structures:

typedef struct objc_cache *Cache
struct objc_cache {
    unsigned int mask /* total = mask + 1 */;
    unsigned int occupied;
    Method _Nullable buckets[1];

CacheOptimized for performance of the method call, object instance whenever a message is received, it is not directly in the isa methods of the class list pointed to traverse search method can respond, because every time searching efficiency is too low, but priority in the Cachelookup.

RuntimeThe system will store the called method Cachein it. If a method is called, it may be called in the future, and it will be more efficient the next time it is searched.

5. Protocol

objc_classThe structure struct objc_protocol_list *protocols(list of protocols to comply with), and the elements stored in it are Protocol(protocols);

typedef struct objc_object Protocol;
struct objc_protocol_list {
    struct objc_protocol_list * _Nullable next;
    long count;
    __unsafe_unretained Protocol * _Nullable list[1];

In objc/runtime.hthe, Protocolit is defined as the (protocol) objc_objectstructure.

#ifdef __OBJC__
@class Protocol;
typedef struct objc_object Protocol;

2. Object

In objc/objc.hthe, Object it is defined as objc_object , the following data structure:

struct objc_object {
    Class _Nonnull isa;  //objc_class    

typedef struct objc_object *id;

objc_object It contains only one Classtype isa . That is, the objc_object only thing saved is Classthe address to which it belongs (class).

3. Meta Class

objc_object The isa points are corresponding to object_class . So object_class ) of isa which points to itself Meta Class .

In Runtime, the type of the class object is called Meta Class , which is used to describe the characteristics of the class object itself. In the methodLists of the metaclass, the method list of the class is saved, which is the so-called "class method". And the class object isa is directed metaclass. Each class object has one and only one metaclass related to it.

4. The relationship between objects, classes, and metaclasses

  1. isa :

    1. Examples of the object isa point corresponding to , the isa point corresponding . (All) metaclass isa final point (NSObject) .
    2. Note :( All) metaclass include (NSObject) , namely, the root metaclass isa point to themselves.
  2. :

  3. The pointing , the pointing (NSObject) , (NSObject) the pointing nil;

  4. The pointing , the pointing (NSObject) , (NSObject) the pointing (NSObject) , (NSObject) the pointing nil.