用来在category里加属性的宏
众所周知,一般的情况下我们是没办法在category里加属性的.
如果想加,需要用到Associated.
@interface NSObject (XYFlyweightTransmit) @property (nonatomic, strong) id uxy_flyweightData; @end @implementation NSObject (UXYFlyweightTransmit) - (id)uxy_flyweightData { return objc_getAssociatedObject(self, NSObject_key_flyweightData); } - (void)setUxy_flyweightData:(id)flyweightData { objc_setAssociatedObject(self, NSObject_key_flyweightData, flyweightData, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end;
如果每次需要写这么多代码才可以实现, 确实很繁琐.
这事可以按照下面的代码封装一下
@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; id currValue = objc_getAssociatedObject( self, propName ); return currValue; } - (id)uxy_retainAssociatedObject:(id)obj forKey:(const char *)key; { const char * propName = key; id oldValue = objc_getAssociatedObject( self, propName ); objc_setAssociatedObject( self, propName, obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); return oldValue; }
这样,我们只需要按照如下方式添加一个属性
- (UIView *)overlay { return [self uxy_getAssociatedObjectForKey:"xy.navigationBar.overlay"]; } - (void)setOverlay:(UIView *)overlay { [self uxy_retainAssociatedObject:overlay forKey:"xy.navigationBar.overlay"]; }
上面的代码看起来还是有不少重复的地方.可以尝试用一个宏来封装一下.
但是当我们真正写宏的时候发现get方法好写, set方法无从下手.因为后面是跟的一个大写的字母.
不漂亮的解决方法:
* 约定属性用大写字母开头
* 约定下划线开头
* 写宏的时候吧setName名字传进去
不过,用的的当然不是上述的方法.
先来看下代码表现起来是怎样的
@interface UINavigationBar (XY) uxy_property_strong(id, test2); @end @implementation UINavigationBar (XY) uxy_def_property_strong(id, test2) @end { self.test2 = @"a"; id c = self.test2; NSLog(@"%@", c); }
实现思路:
在申明属性的时候用setter来修改属性的set方法,在前面加 __ 避开大小写.
具体实现代码如下:
#define uxy_property_strong( __type, __name) \ @property (nonatomic, strong, setter=set__##__name:, getter=__##__name) __type __name; #define uxy_def_property_strong( __type, __name) \ - (__type)__##__name \ { return [self uxy_getAssociatedObjectForKey:#__name]; } \ - (void)set__##__name:(id)__##__name \ { [self uxy_retainAssociatedObject:__##__name forKey:#__name]; }
我们再用元宏metamacro_concat给宏升级一下,让他支持基本类型, 然后取消property前面的@符号, 就有了加属性宏的最终版本uxy_property_basicDataType( __type, __name)
演示代码:
@interface Associated (test) @uxy_property_basicDataType(int, age); @uxy_property_basicDataType(NSTimeInterval, time); @end @implementation Associated (test) uxy_def_property_basicDataType(int, age) uxy_def_property_basicDataType(NSTimeInterval, time) @end // 测试代码 UXY_DESCRIBE( test1 ) { Associated *associated = [[Associated alloc] init]; associated.age = 10; associated.time = 100.5f; UXY_EXPECTED( associated.age == 10 ); UXY_EXPECTED( associated.time == 100.5f ); }
具体实现代码如下:
/** * Returns A and B concatenated after full macro expansion. */ #define metamacro_concat(A, B) \ metamacro_concat_(A, B) #define metamacro_concat_(A, B) A ## B
#define uxy_property_basicDataType( __type, __name) \ property (nonatomic, assign, setter=set__##__name:, getter=__##__name) __type __name; #define uxy_def_property_basicDataType( __type, __name) \ - (__type)__##__name \ { \ NSNumber *number = [self uxy_getAssociatedObjectForKey:#__name]; \ return metamacro_concat(metamacro_concat(__uxy_, __type), _value)( number ); \ } \ - (void)set__##__name:(__type)__##__name \ { \ id value = @(__##__name);\ [self uxy_setAssignAssociatedObject:value forKey:#__name]; \ } #define __uxy_int_value( __nubmer ) [__nubmer intValue] #define __uxy_BOOL_value( __nubmer ) [__nubmer boolValue] #define __uxy_NSTimeInterval_value( __nubmer ) [__nubmer doubleValue]