Add property in Objective C, category, and protocol

Source: Internet
Author: User

The nature of the property is the instance variable + getter and setter method

Category and protocol can add methods

@property keywords can be added in category and protocol

So, when adding property in protocol, it is actually adding getter and setter method, in implementing this Protocol class, we have to manually add the instance variable

Example: @synthesize name = _name; This line of code adds an instance variable and implements a getter, setter method for attributes in the protocol

When adding a property in the category, when adding getter and setter methods in @implentation, because the category cannot add instance variables, you must add the instance variable by using the method of adding associated object at run time

Cases:

 #import  "Uiimage+category.h" Span class= "Hljs-keyword" >static const void *tagKey = & Tagkey;  @implementation uiimage (category)-(nsstring *) tag {return objc_getassociatedobject  (self, Tagkey);} -(void) Settag: (nsstring *) tag {objc_setassociatedobject  (self, Tagkey, tag, Objc_asso ciation_copy_nonatomic);}  @end             

Http://www.jianshu.com/p/00f84e05be49

Add property in Objective C, category, and protocol

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.