通过objc runtime 为类别(Category)动态增加属性
2015-03-26 16:03
489 查看
属性扩展主要用到用OC,APi中函数:objc_setAssociatedObject,objc_getAssociatedObject
原理详细参见官方的https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html
方法扩展用category
首先导入头文件:#import <objc/runtime.h>
示例一
看一个类别和动态添加属性的例子:
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;//设置 key
- (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]);
------------------------------
示例二
static char overviewKey;//设置 key
- (IBAction)showAlertAction:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"warn" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
objc_setAssociatedObject(alert, &overviewKey, @"test", OBJC_ASSOCIATION_RETAIN);
[alert show];
[alert release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
NSLog(@"== : %@",objc_getAssociatedObject(alertView, &overviewKey));
}
}
打印输出
test
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) id objc_getAssociatedObject(id object, const void *key)
原理详细参见官方的https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html
方法扩展用category
首先导入头文件:#import <objc/runtime.h>
示例一
看一个类别和动态添加属性的例子:
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;//设置 key
- (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]);
------------------------------
示例二
static char overviewKey;//设置 key
- (IBAction)showAlertAction:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"warn" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
objc_setAssociatedObject(alert, &overviewKey, @"test", OBJC_ASSOCIATION_RETAIN);
[alert show];
[alert release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
NSLog(@"== : %@",objc_getAssociatedObject(alertView, &overviewKey));
}
}
打印输出
test
相关文章推荐
- objc runtime 动态增加属性
- objc runtime 动态增加属性
- 通过 objc_setAssociatedObject (关联) 的形式实现为Category (类别) 添加属性
- runtime结合category和block动态增加方法以及属性
- ios专题 - objc runtime 动态增加属性
- KVO的使用以及 objc runtime 动态增加属性
- objc runtime 动态增加属性
- objc runtime 动态增加属性
- objc runtime 动态增加属性
- iOS在分类中添加属性,运行时编程,objc runtime 动态增加属性
- objc runtime 动态增加属性
- IOS通过runtime给category添加属性
- iOS runtime 机制 通过别扩展category给一个类添加属性
- ios runtime 动态属性objc_setAssociatedObject和objc_getAssociatedObject
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- python通过__slots__限制类实例动态的增加属性、方法
- iOS通过rumtime给类别添加动态属性
- 通过runtime对类别进行属性的扩展
- 59.通过 runtime为Category添加属性与成员变量