runtime动态添加属性
来源:互联网 发布:python 2.7安装 编辑:程序博客网 时间:2024/03/29 18:38
使用运行时库,必须要先引入 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; - (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]);
判断一个类里是否包含某个属性
+ (BOOL) getVariableWithClass:(Class) myClass varName:(NSString *)name{ unsigned int outCount, i; Ivar *ivars = class_copyIvarList(myClass, &outCount); for (i = 0; i < outCount; i++) { Ivar property = ivars[i]; NSString *keyName = [NSString stringWithCString:ivar_getName(property) encoding:NSUTF8StringEncoding]; keyName = [keyName stringByReplacingOccurrencesOfString:@"_" withString:@""]; if ([keyName isEqualToString:name]) { return YES; } } return NO;}
高级iOS开发技术交流群:415239068,欢迎大家加入
0 0
- runtime动态添加属性
- Runtime 动态添加属性
- runtime -- 动态添加属性
- 动态添加属性(Runtime)
- runtime跳转界面以及动态添加属性
- iOS (Category)runtime动态添加属性
- runtime简单使用之动态添加属性
- Runtime(动态添加属性)学习
- RunTime应用--动态给分类添加属性
- iOS Runtime 动态添加属性不能添加intrinsic类型
- Runtime-动态创建类添加属性和方法
- Runtime-动态创建类添加属性和方法
- iOS开发------runtime关联对象(动态添加属性)
- iOS分类中通过runtime添加动态属性
- Runtime-动态创建类添加属性和方法
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- iOS App设置icon,启动图,App名称的方法
- java中特殊的String类型
- android.content.res.Resources$NotFoundException: String resource ID #0x1
- Android实战技巧之十四:混淆与反编译
- 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数【while循环】
- runtime动态添加属性
- shell十三问之10:&& 与 || 差在哪?
- 淘宝 第三方应用安全规范
- Spring 浅谈
- hdu 3401 Trade (单调队列优化)
- Android input keyevent
- java读取properties配置文件
- unity3d中实现聊天功能
- 8086指令系统