Runtime sorting and runtime sorting
1. Runtime is written in C and sink.
- For the C language, function calls determine which function to call during compilation. For OC functions, they are a dynamic call process. During compilation, they cannot decide which function to call, the corresponding function can be called only when the function is running.
Ii. Common Runtime header files
# Import <objc/runtime. h>
# Import <objc/message. h>
Iii. Common Runtime functions
Class_copyIvarList: obtains the member variable within a class.
Class_copyMethodList: obtains all the methods in a class.
Class_getInstanceMethod: obtains a specific instance method.
Class_getClassMethod: obtains a specific class method.
Class_getMethodImplementation: Get method implementation
Method_exchangeImplementations: implements the exchange of two methods
Objc_msgSend: send messages to objects
Class_addMethod: Add Method
Objc_getAssociatedObject: obtains the associated value based on the associated key.
Objc_setAssociatedObject: Set Association attributes
4. Use Cases of Runtime
Send message
Exchange Method
Dynamically generate attributes and Methods
Call action directly through block callback
Archive and archive
Dictionary-to-Model
5. Specific implementation of various Runtime scenarios
1. Send messages
Note:
2. Exchange Methods
Because the system method, such as imageNamed, cannot be rewritten directly in a category, this will overwrite the system function, and super cannot be called in the category, so the exchange method must be used instead of rewriting.
3. dynamically generate attributes and Methods
If a class has many methods and resources are compared when the class is loaded to the memory, you need to generate a ing table for each method. The dynamic generation method solves this problem.
Dynamically add attributes
4. Call action directly through block callback
// Call action directly through block callback
5. Archive and archive
6. dictionary-to-model conversion
KVC: traverses all keys in the dictionary and searches for corresponding attribute names in the model.
Runtime: traverses all attribute names in the model and searches for them in the dictionary.