Dynamic typing, dynamic binding, dynamic method resolution, introspection

Dynamic type

Dynamic typing means that the specific type of an object is determined at run time.

Dynamic binding

Dynamic binding means that the process of mapping messages to method implementations is done at run time, not compile time.


Dynamic method resolution

Dynamic Method Resolution is a capability that can provide Method implementation dynamically.

One of the dynamic method resolutions is the @dynamic keyword in Objective-C, which tells the compiler that setter and getter methods for this property are provided dynamically at run time.

The message was forwarded three times

The first step is dynamic method resolution

NSObject contains two class methods:

ResolveInstanceMethod: + resolveInstanceMethod: + resolveClassMethod Provides an implementation of an instance method for a given Selector. This method is called every time the instance object receives a message. ResolveClassMethod provides an implementation of a class method for a given Selector. This method is called every time the class receives a messageCopy the code

Copying these two methods provides implementation dynamically at run time for instance methods or class methods.

This function is called when the recipient receives a message method that is not found, giving the object a chance to dynamically add the message method implementation

The second step is to reserve the recipient object

-(id)forwardingTargetForSelector:(SEL)aSelector

If the first method resolution fails to handle the problem, another object B will handle the problem. If object B can handle the message, the message is forwarded.

The third step is to implement the message method in another form

-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

-(void)forwardInvocation:(NSInvocation *)anInvocation

  1. Pass the unknown SEL as a parametermethodSignatureForSelector, process the message in this method, and once it can process,Return method signature(Freely modify method signature,Apple’s signature),Let the follow-upforwardInvocationTo process
  2. We can do a lot of operation in the forwardInvocation, this method is more flexible than forwardingTargetForSelector

    You can do it likeforwardingTargetForSelectorThe same result, except one is for the other object to handle, and the other isSwitch directly to the call target, which is the method’sTarget class

  3. We can also modify the SEL of this method by replacing it with a new SEL





introspection

The NSObject API provides object Introspection. Introspection is the ability to examine information about an object itself at run time.

Because so much of Objective-C’s behavior is done at run time, introspection is critical.

The main introspection methods provided by NSObject are:

IsKindOfClass: Checks whether an object is an instance of a given Class or a subclass of a given Class.

RespondsToSelector: Checks whether an object can respond to a Selector.

ConformsToProtocol: checks whether the object complies with a protocol.

MethodSignatureForSelector: get a Selector to the method signature.





Method to find the

Objc_msgSend goes through the following steps:

  • 1. Use the ISA pointer to find the class of the object

  • 2. Search the class’s objc_cache for the method. If no method is found, proceed to steps 3 and 4

  • Look for methods in the class’s method_list

  • 4. If no method is found in the class, continue in its super class until the root class is found

  • 5. Once the method is found, execute the corresponding method implementation (IMP) and add the method to the method cache

The Runtime introduces a caching mechanism to make method lookups more efficient because, if the method is in the root class, each method call has to go down the inheritance chain through the list of methods of each class, which is inefficient. The implementation of this method cache, which is essentially a hash table, is indexed by the hash of the selector name, and stores the implementation of the method (IMP), which is a very efficient lookup, and if you look at this, you might wonder, since each class maintains a method cache hash table, Why maintain a method list? Isn’t it faster to go straight to the hash table and look up the method every time?

This is because a hash table is an unordered list, whereas a list of methods is an ordered list, and the look-up methods follow the method list. This gives a category the ability to override the implementation of the original class, whereas with a hash table the order is not guaranteed. The principle and implementation of categories will be discussed in a future article.