1. What is a runtime runtime?
Runtime is a relatively low level of pure C language API, belongs to 1 C language Library, contains a lot of the underlying C language API.
In the OC code that we usually write, in the process of running the program, in fact, it turns into the runtime's C language code, runtime is OC's behind-the-scenes workers
For example, in the following method of creating an object, the Alloc method and the Init method are ultimately implemented by sending a message at runtime (message mechanism)
1 // OC: 2 [[Ljperson alloc] init]; 3//Runtime: Import <objc/runtime.h> 4 objc_msgsend with runtime (objc_ Msgsend ("Ljperson", "Alloc"), "Init");
What's the use of 2.runtime?
Runtime is the bottom of the OC and can perform some very low-level operations (with OC is not realistic, bad implementation)
Dynamically create a class during the program's run (for example, the KVO implementation)
Dynamically adding properties \ Methods to a class, modifying property values \ Method \ method implementation during program run
Traverse all member variables (properties) of a class \ All methods
* Code can refer to Mjextention framework
- Exchange two implementations of the methods (mainly used to make some modifications to the system's own methods)
Functions commonly used in 3.runtime:
- Objc_msgsend: Sending a message to an object
- Class_copymethodlist: Traversing all methods of a class
- Class_copyivarlist: Traversing all member variables of a class
- Class_getclassmethod: Obtaining class methods for a class
- Class_getinstancemethod: Obtaining an object method for a class
- Class_getmethodimplementation: Obtaining the implementation of a method of a class ;
- Method_setimplementation: Setting the implementation of a method
- Method_exchangeimplementations: Implementation of two methods of exchange
4. Application Examples:
Waiting to be written ...
Ios:runtime Run time