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:
    • YESThrough theclass_addMethodThe message is processed. End
    • NOAnd into theforwardingTargetForSelector
      • Specify the responseselectorThe message is processed. End
      • Not specifying a responseselector
        • Enter themethodSignatureForSelector, specify method signature, callforwardInvovationThrough theanInvocationDo the processing, the message is processed, end
        • No method signature was specified, the message was not processed, and the system reported an error

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:

  • replaceViewControllerDeclaration 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 changesUINavigationBarthebackButtonItem

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 objectisaPointer finds the class objectid, in the query class objectmethodListsMethod function list
2. If not in good to, alongsuperClassFind the parent class, and then in the parent classmethodListsMethod list
3. Finally findSEL, according to theidandSELconfirmIMP(pointer function), sending a message.
Question:When will it be reportedunrecognized selectorThe mistake?iOSWhat are the mechanisms to avoid this?
1. When sending a message, we use themethodListsList to see what we need to useSELWhen the query is not available, we will always follow the parent class query
2. When the final query is not available, we will reportunrecognized selectorError, called when the system cannot query a method+(BOOL)resolveInstanceMethod:(SEL)selDynamically 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)aSelectorThe 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 theruntimeIn the class structureobjc_ivar_listThe linked list sum of instance variablesinstance_sizeThe memory size of the instance variable has been determined,runtimeWill be calledclass_setvarlayoutorclass_setWeaklvarLayoutTo deal withstrong``weakSo you cannot add instance variables to an existing class.
2. Classes created at runtime can be added to instance variables, calledclass_addIvarFunction, but is callingobjc_allocateClassPairAfter that,objc_registerClassPairBefore, for the same reason.
Question:How does runtime implement automatic nil for weak variables?
1.runtimeFor registered classes, the layout is done forweakObject will put in onehashIn the table. withweakPoints to the memory address of the object askey, when the reference count of this object is0When willdealloc.
2. IfweakThe memory address of the object pointed to isA, then it will beATheta is the bond, right hereweakTable search, find all toAFor the keyweakObject 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)

The last