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