Turn: Objective-c Use @ dynamic and @ synthesize

Source: Internet
Author: User
Document directory
  • Dynamic Method Analysis

@ Dynamic refers to the Code provided by developers: setter is required for read-only attributes, and setter and getter are required for read/write attributes.
@ Synthesize indicates that, unless the developer has already done this, the compiler generates the corresponding code to satisfy the attribute declaration.

After reading some documents, confirm @ dynamic to tell the compiler that the methods for obtaining and assigning attributes are implemented by the user and are not automatically generated.
@ Dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass)

 

Looking at Apple's official documentation, it seems more interesting:

Dynamic Method parsing sometimes requires dynamic implementation of a method. For example, the modifier @ dynamic propertyname before the attribute in objective-C (refer to the attribute section in objective-C 2.0 programming language; indicates that the compiler must dynamically generate the corresponding method for this attribute. You can implement resolveinstancemethod: And resolveclassmethod: to dynamically implement the object methods or class methods for a given object. The objective-C method can be considered as having at least two parameters -- Self and _ cmd --
. You can use the class_addmethod method to add a function to the class method. For example, there are the following functions: void dynamicmethodimp (ID self, Sel _ cmd) {// implementation ....} You can use handler: to implement resolvethismethoddynamically as a class method: @ implementation myclass + (bool) handler :( SEL) Asel {If (Asel = @ selector (resolvethismethoddynamically )) {class_addmethod ([self class], Asel, (IMP) dynamicmethodimp, "V @:"); Return yes;} return [Super resolveinstancemethod: Asel];} @ end generally, message Forwarding (see "message forwarding") and dynamic method parsing are irrelevant. Before entering the message forwarding mechanism, respondstoselector: And instancesrespondtoselector: Will be called first. You can provide an imp for the selected token in the two methods. If you have implemented the resolveinstancemethod: method but still want the normal message forwarding mechanism to be implemented, you only need to return no. Department. m in chapter 30 of cocoa programming for Mac OS X written by Aaron hillegass also uses the dynamic method, that is, the key-value coding method. Source: http://blog.sina.com.cn/s/blog_6f92e3580100zx5s.html
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.