Effective OBJECTIVE-C 2.0-12th: Understanding the message forwarding mechanism

Source: Internet
Author: User
Tags access properties

11 explains the message passing mechanism of the object

12 What happens when a message is received that cannot be read will start the message forwarding mechanism,

  • If the object cannot respond to a selector, it enters the message forwarding process.
  • 1, through the dynamic method of the runtime analysis function, you can use a method when you need to add it to the class.
  • 2, an object can transfer certain selectors that it cannot interpret to other objects for processing.
  • After these two steps, if there is still no way to handle the selection, then start the complete message forwarding mechanism.

Dynamic Method Parsing

After the object receives a message that cannot be read, the following class methods of the class to which it belongs are called first

+ (BOOL) Resolveinstancemethod: (SEL) Selector

The return value is bool, indicating whether the class can add an instance method to handle this selector.

The premise of using this method is: the implementation code of the relevant method has been written well, just waiting for the runtime to plug in the dynamic in the class. This scenario is commonly used to implement @dynamic properties, such as the ability to access properties of Nsmanagedobjects objects in the CoreData framework, because the access methods required to implement these properties can be determined at compile time.

The following code shows how to use "Resolveinstancemethod:" to achieve @dynamic implementation;

Effective OBJECTIVE-C 2.0-12th: Understanding the message forwarding mechanism

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.