category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
属性 其实就是get/set 方法。我们可以使用 objc_setAssociatedObject/objc_getAssociatedObject 实现 动态向类中添加 方法
@interface NSObject (CategoryWithProperty) @property (nonatomic, strong) NSObject *property; @end @implementation NSObject (CategoryWithProperty) - (NSObject *)property { return objc_getAssociatedObject(self, @selector(property)); } - (void)setProperty:(NSObject *)value { objc_setAssociatedObject(self, @selector(property), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end
objective-c中,有类别可以在不修改源码的基础上增加方法;近排在看别人的开源代码时,发现还可以动态增加属性。而且是在运行时,太牛B了。
使用运行时库,必须要先引入 objc/runtime.h
可以使用的函数如下:
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
这个函数
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
兄弟们,看一个类别和动态添加属性的例子:
UILabel+Associate.h
#import <UIKit/UIKit.h> @interface UILabel (Associate) - (void) setFlashColor:(UIColor *) flashColor; - (UIColor *) getFlashColor; @end
UILabel+Associate.m
#import "UILabel+Associate.h" #import <objc/runtime.h> @implementation UILabel (Associate) static char flashColorKey; - (void) setFlashColor:(UIColor *) flashColor{ objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIColor *) getFlashColor{ return objc_getAssociatedObject(self, &flashColorKey); } @end
调用代码:
UILabel *lab = [[UILabel alloc] init]; [lab setFlashColor:[UIColor redColor]]; NSLog(@"%@", [lab getFlashColor]);
//
// UIAlertView+ShowWithBlock.h
#import <UIKit/UIKit.h>
typedef void (^alertBlock)(UIAlertView *alertView, NSInteger buttonIndex);
@interface UIAlertView (UIAlertView_Category)<UIAlertViewDelegate>
-(void)showWithBlock:(alertBlock)block;
@end
// UIAlertView+ShowWithBlock.m
#import "UIAlertView+Category.h"
#import <objc/runtime.h>
static NSString *MyAlertAction = @"MyAlertAction";
@implementation UIAlertView (UIAlertView_Category)
- (void)showWithBlock:(alertBlock)block
{
if (block){
objc_removeAssociatedObjects(self);
objc_setAssociatedObject(self, (__bridge const void *)(MyAlertAction), block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
self.delegate = self;
}
[self show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
alertBlock block = objc_getAssociatedObject(self, (__bridge const void *)(MyAlertAction));
block(alertView, buttonIndex);
}
@end