runtime 获取类的属性,方法,成员变量,以及协议 字典转model 总结
2016-05-19 20:40
495 查看
runtime 的使用笔记
1 runtime 获取类的属性,方法,成员变量,以及协议
2 runtime 简单字典转model 自定义方法
model.h
model.m
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary{
return [[self alloc]initWithDictionary:dictionary];
}
- (instancetype)initWithDictionary:(NSDictionary *)dictionary{
self = [super init];
if (self) {
unsigned int count;
//获取属性列表 遍历并赋值
objc_property_t *propertyList=class_copyPropertyList([self class], &count);
for (unsigned int i=0; i<count; i++) {
const char *propertyName=property_getName(propertyList[i]);
//属性名
NSString *propertyNameStr = [NSString stringWithUTF8String:propertyName];
NSString *propertyValue = [dictionary objectForKey:propertyNameStr];
if ([WYHTools isBlankString:propertyValue]) {
propertyValue = @"暂无";
}
[self setValue:propertyValue forKey:propertyNameStr];
}
}
return self;
}
1 runtime 获取类的属性,方法,成员变量,以及协议
unsigned int count; //获取属性列表 objc_property_t *propertyList=class_copyPropertyList([model class], &count); for (unsigned int i=0; i<count; i++) { const char *propertyName=property_getName(propertyList[i]); NSLog(@"propertyName----->%@",[NSString stringWithUTF8String:propertyName]); NSLog(@"propertyValue----->%@",[model valueForKey:[NSString stringWithUTF8String:propertyName]]); NSLog(@"propertyValueClass----->%@",[[model valueForKey:[NSString stringWithUTF8String:propertyName]] class]); } //获取方法列表 Method *methodList=class_copyMethodList([model class], &count); for (unsigned int i=0; i<count; i++) { Method method=(methodList[i]); NSLog(@"method----->%@",NSStringFromSelector(method_getName(method))); } //获取成员变量列表 Ivar *ivarList=class_copyIvarList([model class], &count); for (unsigned int i=0; i<count; i++) { Ivar myIvar=ivarList[i]; const char *ivarName=ivar_getName(myIvar); NSLog(@"Ivar----->%@",[NSString stringWithUTF8String:ivarName]); } //获取协议列表 __unsafe_unretained Protocol **protocolList=class_copyProtocolList([model class], &count); for (unsigned int i=0; i<count; i++) { Protocol *myProtocal=protocolList[i]; const char *protocolName=protocol_getName(myProtocal); NSLog(@"protocol----->%@",[NSString stringWithUTF8String:protocolName]); }
2 runtime 简单字典转model 自定义方法
model.h
@property (nonatomic ,copy)NSString *propertyA; @property (nonatomic ,copy)NSString *propertyB; @property (nonatomic ,copy)NSString *propertyC; + (instancetype)modelWithDictionary:(NSDictionary *)dictionary;
model.m
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary{
return [[self alloc]initWithDictionary:dictionary];
}
- (instancetype)initWithDictionary:(NSDictionary *)dictionary{
self = [super init];
if (self) {
unsigned int count;
//获取属性列表 遍历并赋值
objc_property_t *propertyList=class_copyPropertyList([self class], &count);
for (unsigned int i=0; i<count; i++) {
const char *propertyName=property_getName(propertyList[i]);
//属性名
NSString *propertyNameStr = [NSString stringWithUTF8String:propertyName];
NSString *propertyValue = [dictionary objectForKey:propertyNameStr];
if ([WYHTools isBlankString:propertyValue]) {
propertyValue = @"暂无";
}
[self setValue:propertyValue forKey:propertyNameStr];
}
}
return self;
}
相关文章推荐
- 如何定义一个getter和seter设置的属性可以被绑定
- Java Runtime Environment 5.0 Update 12 下载
- jQuery获得内容和属性方法及示例
- php set_magic_quotes_runtime() 函数过时解决方法
- Asp.Net 程序错误Runtime Error原因与解决
- js中如何复制一个对象并获取其所有属性和属性对应的值
- System 类 和 Runtime 类的常用用法介绍
- ECMAScript5中的对象存取器属性:getter和setter介绍
- js获取和设置属性的方法
- NET Runtime Optimization Service 1101 错误的解决方法
- 自动释放池的前世今生 ---- 深入解析 autoreleasepool
- 上古时代 Objective-C 中哈希表的实现
- 懒惰的 initialize 方法
- 深入解析 ObjC 中方法的结构
- 你真的了解 load 方法么?
- 从源代码看 ObjC 中消息的发送
- IOS高级教程2:反射根据变量的引用获取变量名
- iOS学习之Objective-C 2.0 运行时系统编程
- AS3 类的定义