Artifice refers to a skill and a product that is too kit and useless.
Reprint please specify the source http://blog.csdn.net/uxyheaven/article/details/46789065
As we all know, in general, we are not able to add attributes to the category.
If you want to add, you need to use associated.
@interface nsobject (xyflyweighttransmit)@property(nonatomic,Strong)IDUxy_flyweightdata;@end @implementation nsobject (uxyflyweighttransmit)- (ID) uxy_flyweightdata{returnObjc_getassociatedobject ( Self, nsobject_key_flyweightdata);} - (void) Setuxy_flyweightdata: (ID) flyweightdata{Objc_setassociatedobject ( Self, Nsobject_key_flyweightdata, Flyweightdata, objc_association_retain_nonatomic);}@end;
If you need to write so much code each time can be implemented, it is really cumbersome.
In general, we will use the following code to encapsulate
@interface nsobject (xy_associated)- (ID) Uxy_getassociatedobjectforkey: (Const Char*) key;-(ID) Uxy_retainassociatedobject: (ID) obj Forkey: (Const Char*) key;-@end @implementation nsobject (xy_associated)- (ID) Uxy_getassociatedobjectforkey: (Const Char*) key{Const Char* propname = key;IDCurrvalue = Objc_getassociatedobject ( Self, propname);returnCurrvalue;} - (ID) Uxy_retainassociatedobject: (ID) obj Forkey: (Const Char*) key; {Const Char* propname = key;IDOldValue = Objc_getassociatedobject ( Self, propname); Objc_setassociatedobject ( Self, propname, obj, objc_association_retain_nonatomic);returnOldValue;}
In this way, we just need to add a property as follows
- (UIView *)overlay{ return [self uxy_getAssociatedObjectForKey:"xy.navigationBar.overlay"];}- (void)setOverlay:(UIView *)overlay{ [self uxy_retainAssociatedObject:overlay forKey:"xy.navigationBar.overlay"];}
The code above looks like there's a lot of repetition, and we're using a macro to encapsulate it.
But when we actually write the macro, we find the Get method is good to write, the set method does not know, because it is followed by a capital letter.
Generally use the following not beautiful method to solve:
* Convention attributes start with capital letters
* Convention underline start
* When you write a macro, SetName's name passes in.
However, since the title of this article is called artifice, the use of of course is not the above method.
Let's take a look at what the code looks like.
@interface uinavigationbar (xy ) uxy_property_ As_associated_strong (id , test2); @end @implementation Uinavigationbar (xy ) Uxy_property_def_associated_strong (id , test2) @end {self Span class= "hljs-variable" >.test2 = @ "a" ; id c = self .test2 ; nslog (@ "%@" , c);}
Implementation ideas:
Modify the property's set method with the setter when declaring the attribute, and avoid the case with the preceding __.
The specific implementation code is as follows:
#define Uxy_property_as_associated_strong (__type , __name) \ @property (nonatomic , strong , Setter=set__# #__name:, getter=__# #__name) __ Type __name; #define Uxy_property_def_associated_strong (__type, __name) \ -(__type) __# #__name \ {return [self uxy_getassociatedobjectforkey: #__name]; } \ -(void ) set__# #__name:(ID) __# #__name \ {[self uxy_retainassociatedobject:__ # #__name Forkey: #__name]; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Objective-c Artifice--a macro used to add properties to the category