IOS message Forwarding

Source: Internet
Author: User

Message Forwarding Delegate and Protocol category Message ForwardingWhen a message is sent to Someobject, but the runtime system does not find the implementation of the corresponding method in both the current class and the parent class, the runtime system does not immediately make an error to cause the program to crash, but instead performs the following steps: Describe the process separately: 1. Dynamic method parsing: Sends a resolveinstancemethod: signal to the current class to check whether a method has been added to the class dynamically. (Search: @dynamic) 2. Fast message forwarding: Check whether the class implements the Forwardingtargetforselector: method, or call this method if it is implemented. If the method returns a value object that is non-nil or non-self, resend the message to the returned object. 3. Standard message forwarding: Runtime sends Methodsignatureforselector: Message gets selector corresponding method signature. The return value is non-null by forwardinvocation: Forwards the message, and the return value is null to send doesnotrecognizeselector: Message to the current object, and the program crashes exiting. As the name implies, we can use the above process in 2, 32 ways to complete message forwarding. Fast message forwarding the implementation of fast message forwarding is simple, only need to rewrite-(ID) Forwardingtargetforselector: (SEL) Aselector method. Let me give you a simple example, such as the existing 2 classes: Teacher and Doctor,doctor can do surgery (operate method).
    1. @interface Teacher:nsobject
    2. @end
    1. @interface Doctor:nsobject
    2. -(void) operate;
    3. @end
With fast message forwarding, it's easy to get teacher to call Doctor's method for surgery. The teacher class needs to be implemented to forward messages to doctor:
    1. -(ID) Forwardingtargetforselector: (SEL) Aselector
    2. {
    3. Doctor *doctor = [[Doctor alloc]init];
    4. if ([Doctor Respondstoselector:aselector]) {
    5. return doctor;
    6. }
    7. return nil;
    8. }
Although the message can be forwarded dynamically, but the static check of the editor is not around, then the question is, since the teacher class does not implement the operate method and how to declare it? So far, I only think of the following 2 ways: Declaring Method 1 ———— category
    1. @interface Teacher (Doctormethod)
    2. -(void) operate;
    3. @end
Declaring Method 2 ———— Importing the header file, calling the teacher class header file to include the doctor header file, telling the compiler to go to Doctor.h to find the declaration of the operator method, and to strongly turn the type when called.
    1. Teacher *teacher = [[Teacher alloc]init];
    2. [(Doctor *) teacher operate];
There is an interest in thinking about a problem: if you turn its type into an ID, you can compile the pass and implement forwarding.    But what is the danger? Method 1 The use of categories is clear and simple enough, why do you want to propose a method 2?       My idea is that the disadvantage of Method 1 is that the method that is thrown out is dead, and is exposed in. h, method 2 is relatively flexible and hides the message I want to forward. Standard message forwarding standard message forwarding requires rewriting of methodsignatureforselector: and Forwardinvocation: two methods. Outgoing Process: Forwarding override method:
  1. -(Nsmethodsignature *) Methodsignatureforselector: (SEL) Aselector
  2. {
  3. nsmethodsignature* signature = [Super Methodsignatureforselector:aselector];
  4. if (signature==nil) {
  5. Signature = [Someobj methodsignatureforselector:aselector];
  6. }
  7. Nsuinteger argcount = [signature numberofarguments];
  8. For (Nsinteger i=0; i<argcount; i++) {
  9. }
  10. return signature;
  11. }
  12. -(void) Forwardinvocation: (nsinvocation *) aninvocation
  13. {
  14. SEL seletor = [aninvocation selector];
  15. if ([Someobj Respondstoselector:seletor]) {
  16. [Aninvocation Invokewithtarget:someobj];
  17. }
  18. }
comparison of two kinds of message forwarding methodsFast message forwarding: simple, fast, but can only be forwarded to an object. Standard message forwarding: Slightly more complex, slower, but the forwarding operation is controllable, can implement multi-object forwarding.
Related Article

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.