Objective-C object model and runtime Mechanism
Content list object model (structure definition, Class Object, Meta class and instance object relationship) message transmission and forwarding mechanism runtime system function understanding Object Model Structure Definition
Object ):The basic building block (OC) is used to store and transmit data.
You can find the definition of the object structure in the objc. h file. The object structure is isa of the Class type, and the Class is the objc_class structure type pointer. Objc_class is the class object structure we understand. It also contains an isa Class Object Structure pointer.
The final implementation of classes and objects is a data structure (subclass is an instance of superclass)
Object Structure
/// Represents an instance of a class.struct objc_object { Class isa OBJC_ISA_AVAILABILITY;};
Id type definition
/// A pointer to an instance of a class.typedef struct objc_object *id;
Class Type Definition
/// An opaque type that represents an Objective-C class.typedef struct objc_class *Class;
Class (object) Structure
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;
Relationships between classes (class objects), meta-classes (meta-class objects), and instance objects
A complete class should include class methods, instance methods, and member variables (instance variables). Each object includes an isa (is a class) pointer pointing to a class Object (The class is determined and corresponding method implementation is called only when the running method is sent to the object message.
), The class object structure records all information about the class.
The isa of A class Object points to the meta class object. The method list in the class object is the instance method (-, instance methods). The method list in the meta class object is the class method (+, class methods)
It can be understood as follows:
Classes include class objects and metadata objects. They are defined by class object structure to form all information of the class. When defining instance objects, no storage space (HEAP) is allocated until the class method alloc function and the instance method init function are called to implement the structure storage allocation of instance objects in the heap, and point isa to its class object. The parent class member variables and the corresponding class Object member variables are initialized to 0 or nil.
The above understanding can be confirmed through the following code and object variable structure analysis. Test code
# Import
# Import
@ Interface AClass: NSObject {int a; char cA;}-(void) printA; @ end @ implementation AClass-(void) printA {NSLog (@ I am class ~);} @ End @ interface BClass: AClass {int B; char cB;}-(void) printB; @ end @ implementation BClass-(void) printB {NSLog (@ I am class B ~);} @ End // ---------- main ---------- int main (int argc, const char * argv []) {@ autoreleasepool {// * Object Model * AClass * a = [[AClass alloc] init]; BClass * B = [[BClass alloc] init]; BClass * b1; [a printA]; [B printB];} return 0 ;}
View the object variable structure (view it in Debug mode by setting the breakpoint)
Isa pointer call illustration for Class Object, Meta class, and Instance Object <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Export + DQo8cD4oMikgw7 + export + DQo8cD4oMykgw7 + 49tSqwOC21M/export/M8YnIgLz4NCjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/150723/0434401T6-1.jpg" title = "\"/> Message transmission and forwarding Messaging ):The process of transferring data between objects and executing tasks
Objective-C is a dynamic language based on the object-oriented feature and message forwarding mechanism. In addition to the compiler, the Runtime system must be used to dynamically create classes and objects for message sending and forwarding.
Different Languages have different function transfer methods. C-function pointer and C ++-function call (reference) class member functions determine the class they belong to during compilation, objective-C uses selector and block.
Objective-C emphasizes message passing rather than method calling. Messages can be transmitted to an object without the need to declare the processing method of these messages during compilation. These methods are determined at runtime. The specific runtime functions are described below.
[receiver message];
It does not immediately execute the code of the message method of the receiver object. Instead, it sends a message to the receiver, which is converted to the following by the compiler:
id obj_msgSend(id self, SEL op, …);
PS: there are still special cases for message calling functions, such as other functions.
Objc_msgSend_stret // structure returned for the message to be sent
Objc_msgSend_fpret // returns a floating point number.
Objc_msgSendSuper // send messages to the superclass
SEL indicates the method selector. The structure is as follows:typedef struct objc_selector*SEL;
, Which can be obtained through the keyword @ selector ()
Id data structure inPart 1: Object Model
.
Obj_msgSend message sending process:
Obtains the corresponding class (class Object) based on the isa class Object Pointer of the Explorer Object. The message method is preferentially searched in the cache (fast map) of the class Object, not find-> go to methodLists (the scheduling table in the class, used for ing methods and actual memory addresses, and the class also contains pointers to the parent class) to find; if it is not found in the class image, go to the super_class search; If the message method is found, Execute its implementation IMP If no message is found, The message is forwarded (message forwarding) Method Data Structure
The runtime. h header file is defined as follows:
Typedef struct objc_method * Method; struct objc_method {SEL method_name; // special string that describes the Method name. You can use the keyword @ selector () to obtain char * method_types; IMP method_imp ;}
PS: Message Forwarding is divided into two major stages: dynamic method resolution (dynamic method resolution) and the complete message forwarding mechanism (full forward mechanism)
Runtime system functions Runtime:After the program runs, the code that provides the relevant support is calledOC runtime Environment (OC runtime)
It provides an important function (such as objc_msgSend) for passing messages between objects and contains all the logic used to create a class instance (that is, to create the storage structure and space of the instance object, including isa pointer to "Class Object)
The runtime system is a dynamic link library written in C language, with the core being message distribution. The Runtime mechanism includes the object model, message transmission and forwarding, method implementation mechanism, and other Runtime methods. It can dynamically create and modify class objects and objects, and implement message transmission and forwarding, method dynamic implementation, Method Swizzling and other functions.