Runtime运行时的简单使用,字典转模型
2016-12-05 11:27
357 查看
自我感觉挺简单的,适合初学者
、、、
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取person类额属性
//使用runtime获取属性列表
NSArray *properties = [Person zl_objecProperties];
NSLog(@"%@",properties);
//字典转模型
Person *person = [Person zl_objcWithDictionary:@{@"name":@"zhangsan",@"age":@23,@"title":@"xigua",@"height":@21}];
NSLog(@"%@",person);
}
、、、
模型类中
、、、
#import <Foundation/Foundation.h>
@interface Person : NSObject
//@property(nonatomic,strong) NSString *name;
//@property(nonatomic,assign) NSInteger age;
//@property(nonatomic,strong) NSString *title;
//@property(nonatomic,assign) double height;
@end
、、、
、、、
#import "Person.h"
@interface Person ()
@property(nonatomic,strong) NSString *name;
@property(nonatomic,assign) NSInteger age;
@property(nonatomic,strong) NSString *title;
@property(nonatomic,assign) double height;
@end
@implementation Person
- (NSString *)description
{
NSArray *key = @[@"name",@"age",@"title",@"height"];
return [[self dictionaryWithValuesForKeys:key]description];
}
@end
、、、
解析的工具类
、、、
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSObject (runtime)
//获取累的属性列表
+ (NSArray *)zl_objecProperties;
//字典转模型
+(instancetype)zl_objcWithDictionary:(NSDictionary *)dic;
@end
、、、
、、、
#import "NSObject+runtime.h"
@implementation NSObject (runtime)
+ (instancetype)zl_objcWithDictionary:(NSDictionary *)dic
{
id obc = [[self alloc]init];
NSArray *properList = [self zl_objecProperties];
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//判断模型中是否有key的属性
if ([properList containsObject:key])
{
[obc setValue:obj forKey:key];
}
}];
return obc;
}
const char *ZLProperLsitKey = "ZLProperLsitKey";
+ (NSArray *)zl_objecProperties
{
/**
获取关联对象 -- 动态增加属性
*/
NSArray *prtList = objc_getAssociatedObject(self, ZLProperLsitKey);
if (prtList != nil)
{
//不用每次调用运行时方法 提高效率
return prtList;
}
//获取属性列表
unsigned int count = 0;
objc_property_t *properList = class_copyPropertyList([self class], &count);
NSLog(@"%d",count);
NSMutableArray *array = [NSMutableArray array];
for (unsigned i = 0; i < count;i++)
{
//从数组中取得属性
//上面是数组所以有*
objc_property_t pry = properList[i];
const char *name = property_getName(pry);
[array addObject:[NSString stringWithFormat:@"%s",name]];
// [array addObject:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];
}
//c语言需要释放
free(properList);
//到此为止获取属性列表结束 领用关联对象,动态增加属性
objc_setAssociatedObject(self, ZLProperLsitKey, array.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return array;
}
@end
、、、
、、、
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取person类额属性
//使用runtime获取属性列表
NSArray *properties = [Person zl_objecProperties];
NSLog(@"%@",properties);
//字典转模型
Person *person = [Person zl_objcWithDictionary:@{@"name":@"zhangsan",@"age":@23,@"title":@"xigua",@"height":@21}];
NSLog(@"%@",person);
}
、、、
模型类中
、、、
#import <Foundation/Foundation.h>
@interface Person : NSObject
//@property(nonatomic,strong) NSString *name;
//@property(nonatomic,assign) NSInteger age;
//@property(nonatomic,strong) NSString *title;
//@property(nonatomic,assign) double height;
@end
、、、
、、、
#import "Person.h"
@interface Person ()
@property(nonatomic,strong) NSString *name;
@property(nonatomic,assign) NSInteger age;
@property(nonatomic,strong) NSString *title;
@property(nonatomic,assign) double height;
@end
@implementation Person
- (NSString *)description
{
NSArray *key = @[@"name",@"age",@"title",@"height"];
return [[self dictionaryWithValuesForKeys:key]description];
}
@end
、、、
解析的工具类
、、、
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSObject (runtime)
//获取累的属性列表
+ (NSArray *)zl_objecProperties;
//字典转模型
+(instancetype)zl_objcWithDictionary:(NSDictionary *)dic;
@end
、、、
、、、
#import "NSObject+runtime.h"
@implementation NSObject (runtime)
+ (instancetype)zl_objcWithDictionary:(NSDictionary *)dic
{
id obc = [[self alloc]init];
NSArray *properList = [self zl_objecProperties];
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//判断模型中是否有key的属性
if ([properList containsObject:key])
{
[obc setValue:obj forKey:key];
}
}];
return obc;
}
const char *ZLProperLsitKey = "ZLProperLsitKey";
+ (NSArray *)zl_objecProperties
{
/**
获取关联对象 -- 动态增加属性
*/
NSArray *prtList = objc_getAssociatedObject(self, ZLProperLsitKey);
if (prtList != nil)
{
//不用每次调用运行时方法 提高效率
return prtList;
}
//获取属性列表
unsigned int count = 0;
objc_property_t *properList = class_copyPropertyList([self class], &count);
NSLog(@"%d",count);
NSMutableArray *array = [NSMutableArray array];
for (unsigned i = 0; i < count;i++)
{
//从数组中取得属性
//上面是数组所以有*
objc_property_t pry = properList[i];
const char *name = property_getName(pry);
[array addObject:[NSString stringWithFormat:@"%s",name]];
// [array addObject:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];
}
//c语言需要释放
free(properList);
//到此为止获取属性列表结束 领用关联对象,动态增加属性
objc_setAssociatedObject(self, ZLProperLsitKey, array.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return array;
}
@end
、、、
相关文章推荐
- XZ_iOS之Runtime使用运行时实现字典转模型和关联对象
- Runtime的使用3 【字典转模型】
- ios开发- 利用运行时(runtime)字典转模型
- iOS-浅谈runtime运行时机制02-runtime简单使用
- iOS-浅谈runtime运行时机制02-runtime简单使用
- Runtime - 运行时 理解和应用 字典转模型框架 AFN中的交换方法
- 使用Swift 字典模型互转 就是这么简单
- 字典转换成模型简单的介绍与使用
- Runtime的使用3 【字典转模型】
- RunTime的运行机制简单使用
- runtime从入门到精通(八)—— 使用runtime实现字典转模型
- iOS 消息机制与运行时 运用runtime字典转模型
- 利用 Runtime(运行时)讲解字典转模型的核心算法
- iOS-浅谈runtime运行时机制02-runtime简单使用
- MJExtension 转换速度快、使用简单方便的字典转模型框架
- iOS-浅谈runtime运行时机制02-runtime简单使用
- iOS-浅谈runtime运行时机制02-runtime简单使用
- iOS-浅谈runtime运行时机制-runtime简单使用(转)
- iOS-浅谈runtime运行时机制02-runtime简单使用
- 墨珞随笔 - IOS - 字典与模型的使用 - 李江辉