Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

runtime

OCWhat do you mean by dynamic runtime language?

  • Dynamic typing: The runtime determines the type of the object, which can pass at compile time, but does not mean there are no problems at run time
  • Dynamic binding: Methods invoked by objects are determined at runtime (message forwarding)
  • Dynamic loading: the implementation of a dynamic library method is not copied into the program, only the reference is recorded, until the relevant method is used to find the implementation in the library

runtimeWhat can be done?

  • Gets the class’s member variables, methods, and protocols
  • Add member variables, methods, and protocols to the class
  • Dynamic change method implementation

class_copyIvarListwithclass_copyPropertyListThe difference between?

  • 1.class_copyIvarListYou can get.hand.mAll properties in the@interfaceVariables declared in braces and property names retrieved are underlined (except in braces).
  • 2,class_copyPropertyListCan only be obtained by@propertyDeclared properties (including.m), the property name obtained is not underlined.

class_ro_tandclass_rw_tThe difference between?

  • class_rw_tProvides the ability to extend classes at runtime,class_rw_tStored in the structureclass_ro_t.
  • class_ro_tIt stores information that has been determined by the class at compile time and is immutable.
  • Both contain information about the class’s methods, attributes (member variables), protocols, and so on, but the lists that store them are implemented differently. In simple termsclass_rw_tStore the two-dimensional array used by the list,class_ro_tUse a one-dimensional array.
  • Methods, properties, protocols, and so on that modify classes at run time are stored inclass_rw_tIn the

What is Method Swizzle and when is it used?

  • Method SwizzleIs to change an existing selector (SEL) corresponding implementation (IMP).
  • A list of class methods is storedSELThe name andIMPThe mapping relation of.
  • Developers can leveragemethod_exchangeImplementationsTo swap the two methodsIMP
  • Developers can leveragemethod_setImplementationTo set the IMP of a method directly
  • This can be changed at run timeSELandIMPTo achieve method substitution.

Method SwizzleMatters needing attention

  • In order to ensure thatSwizzle MethodMethod substitution must be performed on the call, which can be performed inloadPerformed in the
  • +loadDo not call when used inside[super load]. If I call it multiple times[super load], may appear “Swizzle ineffective” illusion
  • Avoid calling[super load]Lead toSwizzlingMultiple executions, inloadThe use ofdispatch_onceEnsure that the exchange is performed only once.
  • Subclasses that replace unimplemented inheritance methods can replace the implementation in the parent class, affecting the parent class and other children
  • +initializeInside use to adddispatch_once
  • Some checks need to be done during version iteration in case the library functions change

How tohookMethods of one object without affecting other objects

  • Method 1: Create a new subclass override method
  • Method 2: Make this object’s class follow a protocol,hookWhen the judgment. The drawback is that other objects that follow this agreement will suffer.
  • Method 3: Create a new subclass at run time and modify the objectisaPointers to subclasses,hookThe use of isKindOfDetermine type

Message is sent

Message mechanism

  • 1, fast search, method cache
  • 2, slow search, method list
  • 3. Message forwarding
    • 3-1. Dynamic analysis of methods,resolveInstanceMethod
    • 3-2. Fast message forwardingforwardingTargetForSelector
    • 3-3. Standard message forwarding,methodSignatureForSelector & forwardInvocation

What happens when you send a message to a nil object in objC?

  • Looking for someoneisaPointer returns the address0x0, do not do any operations, there will be no errors.

What happens when OBJC sends a message to an object?

  • A method call actually sends a message through the callobjc_msgSend()The implementation.
  • First of all, throughobjtheisaPointer finds the correspondingclass.
  • Then, start the quick lookup process. inclassList of cache methods (objc_cache) to find the method, if found directly return the correspondingIMP.
  • If it is not found in the cache, the slow lookup process begins. inclasstheMethod ListFind the corresponding method, find the return correspondingIMP.
  • If they can’t find it, they go through the message forwarding process

_objc_msgForwardWhat does a function do?

  • _objc_msgForwardUsed for message forwarding: called when a message is sent to an object, but it is not implemented_objc_msgForwardTry to do message forwarding.

Why do I need to do method caching?

  • Look it up every time you execute this methodMethod ListToo much performance.
  • useobjc_cacheMake a cache of the methods that have been calledmethod_nameAs akey.method_IMPAs avalue.
  • Next time you receive a message, just passobjc_cacheTo find the correspondingIMPJust avoid going through it every timeobjc_method_list

What if I can’t find a way all the time?

  • It triggers a message forwarding mechanism, and we have three chances to fix it to prevent itcrash
  • The dynamic resolution of the method, throughresolveInstanceMethodAdd an IMP to make it run.
  • Fast message forwarding, inforwardingTargetForSelectorReturns an object that can execute the method.
  • Standard message forwarding,methodSignatureForSelectorCreate a method signature of the same method type (NSMethodSignature), and rewrite itforwardInvocationAnd assigns the method that owns the signature toanInvocation.selector.

The pros and cons of message forwarding mechanisms

  • Advantages: The message forwarding mechanism provides the opportunity for a remedy when no method can be found.
  • Disadvantages: Generally, crash processing is performed in the base class, so some crashes may be ignored and the problem cannot be exposed.

IMP,SEL,MethodThe differences and usage scenarios

  • SELA code name used to find a method, handle notifications/timers, etc
  • IMPIs a pointer to the implementation of a method, used for dynamic method resolution
  • MethodIt’s an object. It’s in thereSELandIMPIs used when the message forwarding process obtains the method signature