category
2016-07-12 15:33
525 查看
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
相关文章推荐
- ssh-host-config 28: syntax error: got (, expecting Newline
- Google的Google Chart Tools使用
- Django高级视图和URL配置
- ubuntu 16.04 LTS 下Robomongo 无法输入中文的问题
- 大话设计模式(golang) 三、装饰模式
- Sunday Algorithm
- 在Django中使用DISTINCT
- ORA-01017: invalid username/password; logon denied 解决办法
- 在Leangoo里怎么修改昵称,简称,头像?
- Django程序开发基本步骤
- Leangoo用户设置在哪里?
- USACO - Oct08 Gold Watering Hole 挖井 Kruskal演算法+点权转边权
- 在Django中使用DISTINCT
- django工作原理
- Google的高级搜索
- Django模板系统
- golang之cgo一---go与C基本类型转换
- 2天值75亿美元?AR手游Pokemon Go全球爆红背后的5大原因
- 优化 Go 中的 map 并发存取
- 5个GoLang 应用优化措施