IOS-Runtime (re-exploration of message transmission)

Source: Internet
Author: User

1. Message Search Optimization
So far, we have understood the message passing process in Objective-c. We found that if every function call goes through the above process (the function call efficiency will be very low, especially when there are many inheritance layers of a class, the efficiency of searching for the class will be lower. In order to speed up the search and call speed, Objective-c optimizes the message search.
The class object in the previous section knows that it contains a struct objc_cache * cache Member, which is used to improve the search efficiency. Each class has its own cache, including the inherited methods and the methods defined in the class. When we look for IMP:

1. First, search for the method cache of this class. If yes, return it.
2. If not found, go to the method list of the class. If it is found in the method list of this class, IMP is returned and added to the cache. According to the recent usage principle, this method is highly likely to be called again, and the cache can save the overhead of the next call and re-query.
3. if the corresponding IMP is not found in the method list of this class, find it in the method list of its parent class structure through the super_class pointer in this class structure, find the corresponding imp in the method list of a parent class, return it, and add it to the cache.
4. If the corresponding IMP is not found in the method list of itself and all parent classes, go to the Message forwarding process described below.

2. Message forwardingAn error occurs when you send messages to an object that cannot be processed. However, before the Objective-C runtime system throws an error, A special message forwardInvocation is sent to the message receiving object to notify the object. The unique parameter of the message is an NSInvocation object, which encapsulates the parameters of the original message and message.
We can implement forwardInvocation: The method to perform some default processing for messages that cannot be processed, or to forward messages to other objects for processing without throwing an error.
For the role of message forwarding, consider the following scenarios: Suppose we need to design an object that can respond to the negotiate message and include responses of other types of objects to the message. Through the implementation of the negotiate method, it is easy to forward the negotiate message to other objects. Furthermore, let's assume that we want our object to be exactly the same as the response of another class object to the negotiate message. One possible way is to let our class inherit the implementation of methods of other classes. However, sometimes this method is not feasible, because our class and other classes may need to respond to the negotiate message in different inheritance systems. Although our class cannot inherit the negotiate method of other classes, we can still provide a method implementation, which simply forwards the negotiate message to other class objects, it's like "borrowing" from other classes. -Negotiate {if ([someOtherObject respondsToSelector: @ selector (negotiate)]) return [someOtherObject negotiate];
Return self ;}
This method is not flexible, especially when many messages want to be transmitted to other objects, we must provide methods for each message. In addition, this method cannot process unknown messages. When we write the code, all the sets of messages we need to forward must be determined. However, in fact, this set will change with the occurrence of running events, the definition of new methods or classes. ForwardInvocation: a message provides a more special and dynamic solution to this problem: when an object cannot respond to a message because it does not have a corresponding implementation method, at runtime, the system will send a message to this object through forwardInvocation. Each object inherits the forwardInvocation: method from the NSObject class. However, the method implementation in NSObject simply calls doesNotRecognizeSelector :. By implementing our own forwardInvocation: method, we can forward messages to other objects in this method. To forward messages to other objects, the forwardInvocation method must do the following: 1. decide who to forward the message to, and 2. you can use the invokeWithTarget: Method to forward a message out of the original parameter:-(Void) condition :( NSInvocation *) anInvocation {if ([someOtherObject respondsToSelector: [anInvocation selector]) [anInvocation condition: someOtherObject]; else [super condition: anInvocation];}
The returned value after the message is forwarded is returned to the original message sender. You can return any type of return values, including id, struct, and floating point. ForwardInvocation: A method is like a distribution center for unrecognized messages and forwards these messages to different recipients. Or it can send all messages to the same receiving object as a transport station. It can translate a message into another message, or simply "eat" some messages, so there is no response or error. ForwardInvocation: The method can also provide the same response to different messages, depending on the specific implementation of the method. This method provides the ability to link different objects to the message chain.
Note: forwardInvocation: The method is called only when the message receiving object cannot respond to the Message normally. Therefore, if we want an object to forward the negotiate message to other objects, this object cannot have the negotiate method. Otherwise, forwardInvocation will not be called.

3. Now, when we look back at the example. The runtime of IOS is quite powerful. Through learning the runtime, I have a deeper understanding of the Objective-c type system. Actually, anyone familiar with Python should also know that Python is a fully dynamic language, and its type system is somewhat similar to Objective-c, give me time to compare it with Objective-c in detail.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.