Good articles to my personal technology blog: https://cainluo.github.io/15077238804616.html
At this point, the knowledge and applications of the RunTime are basically covered. The rest is up to you to apply and accumulate in your own projects.
This article is mainly about
Reprint statement: if you need to reprint this article, please contact the author, and indicate the source, and can not modify this article without authorization.
The RunTime based
Basic path diagram:
There are a few important things to be clear about as we look at the basics of RunTime:
- SEL
- id
- Class
- Method
- Ivar
- IMP
- Cache
- Property
We can get all the information about a given class from these things, whether public or private, all of it, and manipulate it.
PS: But when working with private methods, be careful not to use them on shelves unless you have a way to get them approved by Apple.
The RunTime advanced
Advanced path diagram:
As we learn more about RunTime, we need to learn more about it.
Message mechanism:
- objc_msgSend
- objc_msgSend_fpret
- objc_msgSend_stret
- objc_msgSendSuper
- objc_msgSendSuper_stret
Object association:
- objc_setAssociatedObject()
- objc_getAssociatedObject()
- objc_removeAssociatedObjects()
Object association policy:
- OBJC_ASSOCIATION_ASSIGN
- OBJC_ASSOCIATION_RETAIN_NONATOMIC
- OBJC_ASSOCIATION_COPY_NONATOMIC
- OBJC_ASSOCIATION_RETAIN
- OBJC_ASSOCIATION_COPY
Dynamic method parsing:
resolveInstanceMethod:
YES
Through theclass_addMethod
The message is processed. EndNO
And into theforwardingTargetForSelector
- Specify the response
selector
The message is processed. End - Not specifying a response
selector
- Enter the
methodSignatureForSelector
, specify method signature, callforwardInvovation
Through theanInvocation
Do the processing, the message is processed, end - No method signature was specified, the message was not processed, and the system reported an error
- Enter the
- Specify the response
The RunTime application
Application path diagram:
After learning about RunTime, we can apply it to our actual development.
Category
- associations
- Control object
Class
- Dynamic addition method
- Dynamic switching method
- Intercept and replace methods dynamically
- Add extra functionality to a method dynamically
Model
- Automatic filing and filing
- Automatic dictionary to model
- Dictionary to model (model attributes more than dictionary keys)
- Dictionary to model (nested model within model)
- Dictionary to model (nested model in array)
RunTime instance development scenario
In real development, we have some example scenarios that use RunTime:
- replace
ViewController
Declaration cycle of - Solve the problem of collection class index crash
- Prevent the button from repeated high-intensity clicking
- Global replacement control initial effect
- App hot repair
- Abnormal App loading booth map
- Global changes
UINavigationBar
thebackButtonItem
Runtime Method Swizzling Development instance summary
RunTime interview questions and answers
Question:What happens when OBJC sends a message to an object? |
---|
1. According to the objectisa Pointer finds the class objectid , in the query class objectmethodLists Method function list |
2. If not in good to, alongsuperClass Find the parent class, and then in the parent classmethodLists Method list |
3. Finally findSEL , according to theid andSEL confirmIMP (pointer function), sending a message. |
Question:When will it be reportedunrecognized selector The mistake?iOS What are the mechanisms to avoid this? |
---|
1. When sending a message, we use themethodLists List to see what we need to useSEL When the query is not available, we will always follow the parent class query |
2. When the final query is not available, we will reportunrecognized selector Error, called when the system cannot query a method+(BOOL)resolveInstanceMethod:(SEL)sel Dynamically interpreted methods to give me a chance to add methods that can’t be called. |
3. Or we could use it again-(id)forwardingTargetForSelector:(SEL)aSelector The redirection method tells the system what method to call so it doesn’t crash. |
Question:Can I add instance variables to the compiled class? Can I add instance variables to classes created at run time? Why is that? |
---|
1. You cannot add instance variables to compiled classes. |
2. Can add instance variables to classes created at run time. |
Explanation: |
1. The compiled class is registered in theruntime In the class structureobjc_ivar_list The linked list sum of instance variablesinstance_size The memory size of the instance variable has been determined,runtime Will be calledclass_setvarlayout orclass_setWeaklvarLayout To deal withstrong``weak So you cannot add instance variables to an existing class. |
2. Classes created at runtime can be added to instance variables, calledclass_addIvar Function, but is callingobjc_allocateClassPair After that,objc_registerClassPair Before, for the same reason. |
Question:How does runtime implement automatic nil for weak variables? |
---|
1.runtime For registered classes, the layout is done forweak Object will put in onehash In the table. withweak Points to the memory address of the object askey , when the reference count of this object is0 When willdealloc . |
2. Ifweak The memory address of the object pointed to isA , then it will beA Theta is the bond, right hereweak Table search, find all toA For the keyweak Object to set tonil . |
Question:What elements in the class structure change when you add an attribute to a class |
---|
1.instance_size : Memory size of the instance. |
2.objc_ivar_list *ivars : Attribute list. |
conclusion
Well, finally to the end, I hope you can learn knowledge in my article, marry Bai Fumei as soon as possible, onto the peak of life, and finally attach all the articles:
RunTime Quick Start
- IOS Development: How to Make iOS Development Work
- IOS Development: How to Make iOS Development Work – RunTime
- IOS Development – RunTime(3)
The RunTime application
- How to Make iOS Development work (Part 1)
- How to Make iOS Development work (Part 2)
- IOS Development: An App with RunTime (Part 3)