ObjcExtensionProperty
概述
ObjcExtensionProperty 是一种宏定义集合,用于为 Objective-C 类和扩展添加 getter 和 setter 方法。
- 一行代码即可快速添加 getter/setter 方法
- 为类扩展添加弱属性(同样只需一行代码)
- 一行代码即可轻松添加懒加载属性 getter 方法
用法
使用以下任何宏定义来实现您的 getter/setter(s)
//instance variable backed lazy getter method macro define
#define __GETTER_LAZY_IVAR(Class,name,initializer...)
//dynamic property lazy getter method
#define __GETTER_LAZY(Class,name,initializer...)
//common object type property setter method
#define __SETTER(name,setter,association)
//common object type property getter method
#define __GETTER(Class,name) -(Class *)name
//common object type property setter method with additional costomize code
#define __SETTER_CUSTOMIZE(name,setter,association,customizeCode...)
//common object type property getter method provide a default return value
#define __GETTER_DEFAULT(Class,name,defaultValue)
//common weak object type property setter method
#define __SETTER_WEAK(name,setter)
//common weak object type property getter method
#define __GETTER_WEAK(Class,name)
//primitive type property setter method
#define __SETTER_PRIMITIVE(type,name,setter,NSNumberMethod)
//primitive type property getter method
#define __GETTER_PRIMITIVE(type,name,NSNumberMethod)
//primitive type property getter method with a default return value
#define __GETTER_PRIMITIVE_DEFAULT(type,name,defaultValue,NSNumberMethod)
示例
为类扩展添加各种类型的属性
让我们看看通过ObjcExtensionProperty添加各种属性有多简单。在这个例子中,我们假设你想要为UIView
添加一些属性。
//UIView+DyanmicTest.h
@interface UIView (DyanmicTest)
@property (nonatomic, strong) NSString *dynProperty; //common object property
@property (nonatomic) int dynPrimitiveValue; //primitive type property
@property (nonatomic, weak) NSString *dynWeakProperty; //weak property
@property (nonatomic, lazy) NSString *lazyProperty; //lazy property
@property (nonatomic) CGFloat *dynHeight; //default is 480.0
//check the code in .m file
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic) CGFloat addHeight;
@end
在.m文件中为属性添加支持的后台获取/设置方法
//UIView+DyanmicTest.m
@implementation UIView (DyanmicTest)
//common getter/setter
__SETTER(dynProperty, setDynProperty:, OBJC_ASSOCIATION_RETAIN)
__GETTER(NSString, dynProperty)
//primitive type getter/setter
__SETTER_PRIMITIVE(int,dynPrimitiveValue, setDynPrimitiveValue:, numberWithInt:)
__GETTER_PRIMITIVE(int, dynPrimitiveValue, intValue)
//weak reference getter/setter
__SETTER_WEAK(dynWeakProperty, setDynWeakProperty:)
__GETTER_WEAK(NSString, dynWeakProperty)
//lazy property getter
__GETTER_LAZY(NSString, lazyProperty,[NSString stringWithFormat:@"lazy created on %@",[NSDate date]])
//getter with default return value
__SETTER_PRIMITIVE(CGFloat, dynHeight, setDynHeight:, numberWithDouble:)
__GETTER_PRIMITIVE_DEFAULT(CGFloat, dynHeight, 480.0, doubleValue)
//setter with customize code
__SETTER_CUSTOMIZE(tableView, setTableView:, OBJC_ASSOCIATION_RETAIN, {
UITableView *tbv = tableView;
tbv.dataSource = self;
tbv.delegate = self;
})
__GETTER(UITableView, tableView)
//primitive setter with cusomize code
__SETTER_PRIMITIVE_CUSTOMIZE(CGFloat, addHeight, seAddHeight:, numberWithDouble:, {
self.frame = CGRectMake(0, 0, 320, self.frame.size.height+addHeight);
})
__GETTER_PRIMITIVE(CGFloat, addHeight, doubleValue)
@end
没有ObjcExtensionProperty,你会发现实现这些获取/设置方法非常麻烦,你需要写更多的代码,尤其是对于懒属性。
//UIView+DyanmicTest.m
@implementation UIView (DyanmicTest)
//lazyProperty's getter method without ObjcExtensionProperty
//define a key
const char lazyPropertyKey;
// implement the getter
-(NSString *)lazyProperty{
NSString *retValue = objc_getAssociatedObject(self, &lazyPropertyKey);
if(!retValue) {
retValue = [NSString stringWithFormat:@"lazy created on %@",[NSDate date]];
objc_setAssociatedObject(self, &lazyPropertyKey, retValue, OBJC_ASSOCIATION_RETAIN);
}
return retValue;
}
.
//other properties getter/setter methods
.
.
.
.
@end
为类扩展添加弱属性
objc_setAssociatedObject()
不提供弱引用类型,而是使用OBJC_ASSOCIATION_ASSIGN,这意味着当你访问之前已经释放的对象时,你肯定会遇到崩溃。使用ObjcExtensionProperty,你不再需要面对这样的问题。
//define weak property in extension's .h file
@property (nonatomic, weak) NSString *dynWeakProperty; //weak property
//implement weak property's getter/setter methods
__SETTER_WEAK(dynWeakProperty, setDynWeakProperty)
__GETTER_WEAK(NSString, dynWeakProperty)
添加懒属性的获取方法
ObjcExtensionProperty可以轻松地为懒属性添加获取方法,这既适用于通过__GETTER_LAZY_IVAR()
实现的ivar支持的属性,也适用于类扩展(通过__GETTER_LAZY
)。
ivar支持的属性的懒获取方法
//define lazy property
@interface RRViewController ()
@property (nonatomic,lazy) HttpClient *client;
@end
//
@implementation RRViewController
{
HttpClient *_client;
}
//lazy getter method for self.client
__GETTER_LAZY_IVAR(HttpClient, client, [[HttpClient alloc]initWithUrl:@"https://github.com/Roen-Ro"])
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"log _client.url:%@",_client.url); //null
NSLog(@"log self.client.url:%@",self.client.url); //https://github.com/Roen-Ro
}
//....
@end
要求
ios 5.0, mac os 9.0
安装
ObjcExtensionProperty可通过CocoaPods获取。要安装它,请简单地将以下行添加到您的Podfile中:
pod 'ObjcExtensionProperty'
或将“ObjcExtensionProperty”目录中的文件直接拖放到您的Xcode项目中。
作者
罗亮富, [电子邮件地址被隐藏,请点击这里查看]
许可证
ObjcExtensionProperty 可在 MIT 许可下使用。详细信息请参阅 LICENSE 文件。