One, what is objective-c runtime?
Simply put, the OBJECTIVE-C runtime is a C library that implements the Objective-c language. objects can be represented in the structure of the C language, whereas methods (methods) can be implemented with C functions. In fact, they did almost as well, plus some extra features. Once these structures and functions are encapsulated by the runtime function, OBJECTIVE-C programmers can create, inspect, modify classes, objects, and their methods while the program is running.
In addition to encapsulation, the OBJECTIVE-C Runtime Library is also responsible for locating the final execution code of the method. When the program executes [object DoSomething], the method is not directly found and called. Instead, a message is sent to the object (here, we usually call it the recipient). The runtime library gives a chance for the object to decide what response to make based on the message. Alan Kay repeatedly emphasized that message passing (message-passing) is the most important part of Smalltalk (Objective-c based on Smalltalk), not the object.
Objective-c is a runtime-oriented language. So the question is, what is a runtime language? A runtime language is a language that determines what is implemented within a function and makes other decisions when the application is running. Is objective-c a run-time language? No. It is a runtime-oriented language, which means that whenever possible, it delays the decision-making time from compile time and link time until the code actually executes. As noted earlier, Cocoa provides the runtime environment required for iOS applications
Ii.messages in runtime
①message (Message)
The specific definition of the message is hard to say, because there is no real code description, and a simple message is an abstraction that includes the function name + argument list, and he does not have an actual entity.
②method (method)
method is the real code of existence. such as:-(int) meaning {return 42;}
③selector (method selector)
Selector exists through the SEL type, describing a specific method or message. In the actual programming, the retrieval method can be done by selector.
Three,_cmd key Words
- (void)message { self.name = @"James";//通过self关键字给当前对象的属性赋值 SEL currentSel = _cmd;//通过_cmd关键字取到当前函数对应的SEL NSLog(@"currentSel is :%s",(char *)currentSel);
Printing results:
ObjcRunTime[693:403]currentSelis:message
Iv.definition of Class
typedefstruct objc_class *Class;typedefstruct objc_object { Class isa;} *id
We can see here there are two structures, a class structure, an object structure. All Objc_object object structures have an Isa pointer that points to the class to which it belongs, and this pointer is used at run time to detect if the object can respond to a selector. It's over. We see an ID pointer at the end. This pointer is simply used to represent a OBJC object, a bit similar to C + + generics. Once you have an ID pointer, you can get the class of the object and detect if it responds to a selector. This is the usual way to call a delegate.
V.IMP (Method implementations)
typedef id (*IMP)(id self,SEL _cmd,...
An IMP is a function pointer, which is generated by the compiler, and when you start a OBJC message, the code that eventually executes is specified by the function pointer.
Reference Links:
1. Understanding Objective-c Runtime
2, iOS development in-depth discussion of runtime mechanism 01-class and object
3, OBJECTIVE-C Runtime's basic characteristics (I.)
4. News of Objective-c Runtime (ii)
5, iOS Development: detailed OBJECTIVE-C Runtime
6. What is the difference between the runtime of Objective-c and the runtime of Java?
IOS Runtime Learning Notes