- Notifications
You must be signed in to change notification settings - Fork1
Convenient objc getter/setter methods adding for classes and categories, add weak property to categories, one line code for lazy property's getter
License
Roen-Ro/ObjcExtensionProperty
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
ObjcExtensionProperty is a collection of macro defines for adding getter and setter methods for objective-c calsses and extensions.
- Fast adding getter/setter methods with just one line code
- Add weak properties for calss extension (also with just one line code)
- Easy to add lazy property getter method with just one line code
use any of the macro definions below to implement your 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)
Let see how easy it is to add various kinds of properties thoughObjcExtensionProperty. In this example we assume that you want to add some properties forUIView
//UIView+DyanmicTest.h@interfaceUIView (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
add backed getter/setter methods for properties in .m file
//UIView+DyanmicTest.m@implementationUIView (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,[NSStringstringWithFormat:@"lazy created on%@",[NSDatedate]])//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
WithoutObjcExtensionProperty, you'll find that implementing these getter/setter methods is cumbersome, you will have to write much more code. especially for lazy properties.
//UIView+DyanmicTest.m@implementationUIView (DyanmicTest)//lazyProperty's getter method without ObjcExtensionProperty//define a keyconstchar lazyPropertyKey;// implement the getter-(NSString *)lazyProperty{NSString *retValue =objc_getAssociatedObject(self, &lazyPropertyKey);if(!retValue) { retValue = [NSStringstringWithFormat:@"lazy created on%@",[NSDatedate]];objc_setAssociatedObject(self, &lazyPropertyKey, retValue,OBJC_ASSOCIATION_RETAIN); }return retValue;}.//other properties getter/setter methods....@end
objc_setAssociatedObject()
doesn't provide a weak reference type, but instead with OBJC_ASSOCIATION_ASSIGN, which means whenever you access to a object that was previousely deallocated, you will cetainly got a crash. withObjcExtensionProperty you don't have to face such a problem anymore.
//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 can easy to add getter method for lazy properties both for ivar backed properties(through__GETTER_LAZY_IVAR()
) and in calss extensions(through__GETTER_LAZY
)
lazy getter method for ivar backed property
//define lazy property@interfaceRRViewController ()@property (nonatomic,lazy) HttpClient *client;@end//@implementationRRViewController{ HttpClient *_client;}//lazy getter method for self.client__GETTER_LAZY_IVAR(HttpClient, client, [[HttpClientalloc]initWithUrl:@"https://github.com/Roen-Ro"])- (void)viewDidLoad{ [superviewDidLoad];NSLog(@"log _client.url:%@",_client.url);//nullNSLog(@"log self.client.url:%@",self.client.url);//https://github.com/Roen-Ro}//....@end
ios 5.0, mac os 9.0
ObjcExtensionProperty is available throughCocoaPods. To installit, simply add the following line to your Podfile:
pod'ObjcExtensionProperty'
Or drag and drop files in 'ObjcExtensionProperty' directory directly into your xcode projects
Roen(罗亮富),zxllf23@163.com
ObjcExtensionProperty is available under the MIT license. See the LICENSE file for more info.
About
Convenient objc getter/setter methods adding for classes and categories, add weak property to categories, one line code for lazy property's getter
Resources
License
Uh oh!
There was an error while loading.Please reload this page.