470D79AC69E3D1048C00A078FD5D835E.jpg
分享一個(gè)使用runtime進(jìn)行json轉(zhuǎn)model的方法, 有很多開(kāi)源庫(kù), 比如YYModel, MJExtension等等, 都可用于json解析, 但是我做iOS開(kāi)發(fā)兩年的時(shí)間里用的都是runtime封裝的方法, 覺(jué)得很好用, 也沒(méi)有出現(xiàn)問(wèn)題.
@interface QSMObject : NSObject
//**
* 傳入一個(gè)字典, 返回值的當(dāng)前傳入的字典對(duì)應(yīng)的model值
*/
- (id)initWithDic:(NSDictionary *)dic;
//**
* 傳入一個(gè)數(shù)組, 數(shù)組對(duì)應(yīng)的是很多歌字典, 返回值的數(shù)組對(duì)應(yīng)的每個(gè)字典所對(duì)應(yīng)的model數(shù)組
*/
+ (NSMutableArray *) getListArray:(NSArray *) aArray;
@end
#import <objc/runtime.h> //引入runtime
@implementation QSMObject
- (id)initWithDic:(NSDictionary *)dic {
if (self = [super init]) {
unsigned int outCount = 0;
Class currentClass = [self class];
while (currentClass) {
//獲取當(dāng)前類所有的屬性
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (int i = 0; i < outCount; i++) {
//獲取該類的一個(gè)屬性指針
objc_property_t property = properties[i];;
//property_getName(property) 獲取屬性的名稱
//initWithCString, c的字符串轉(zhuǎn)OC的字符串
NSString *propertyNameString =[[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
//因?yàn)閕d是一個(gè)OC的關(guān)鍵字, 你不能聲明一個(gè)屬性名稱為id, 所以遇到屬性名稱是id就攜程id_i
//如果屬性名稱是id_i, 則把id賦值給當(dāng)前屬性
if ([propertyNameString isEqualToString:@"id_i"]) {
propertyNameString = @"id";
}
id value;
//判斷傳過(guò)來(lái)的參數(shù)是不是字典類型
if ([dic isKindOfClass:[NSDictionary class]]) {
//根據(jù)key獲取json數(shù)據(jù)對(duì)應(yīng)的value
//如果對(duì)應(yīng)的可以不存在, 使用字典去獲取一個(gè)value值, 則返回值為nil
value = [dic objectForKey:propertyNameString];
}
//如果該值不存在
if (value == [NSNull null]|| value == nil) {
//則返回繼續(xù)尋找
continue;
}
//如果值存在
if (value) {
//但度處理名稱為id的
if ([propertyNameString isEqualToString:@"id"]) {
[self setValue:[dic objectForKey:@"id"] forKey:@"id_i"];
}else{
//根據(jù)屬性, 使用kvc賦值
[self setValue:value forKey:propertyNameString];
}
}
}
//最后別忘記釋放
free(properties);
Class superclass = [currentClass superclass];
currentClass = superclass;
}
}
return self;
}
+ (NSMutableArray *) getListArray:(NSArray *) aArray
{
if (aArray == nil) {
return [NSMutableArray array];
}
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for(NSDictionary *dic in aArray)
{
[dataArray addObject:[[self alloc]initWithDic:dic]];
}
return dataArray;
}
+ (NSMutableArray *) getListArray:(NSArray *) aArray
{
if (aArray == nil) {
return [NSMutableArray array];
}
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for(NSDictionary *dic in aArray)
{
[dataArray addObject:[[self alloc]initWithDic:dic]];
}
return dataArray;
}
@end
如何使用
假如從服務(wù)器上獲取的數(shù)據(jù)是這樣的
/**
school = "xx大學(xué)";
group = {
teacher = "王老師";
classNo = 17;
};
personlist= [
{
name = "小名";
sex = "男";
};
{
name = "小麗";
sex = "女";
};
];
*/
首先創(chuàng)建對(duì)應(yīng)的mode類
@class GroupModel, PersonModel;
@interface TheClass : JSObject
@property (nonatomic, strong)NSString *school;
@property (nonatomic, strong)GroupModel *group; //注意此處的類型
@property (nonatomic, strong)NSArray <PersonModel *>*personlist;
@end
@interface GroupModel : JSObject
@property (nonatomic, strong)NSString *teacher;
@property (nonatomic, assign)NSInteger classNo;
@end
@interface PersonModel : JSObject
@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *sex;
@end
@implementation TheClass
- (id)initWithDic:(NSDictionary *)dic {
self = [super initWithDic:dic];
if (self) {
if ([dic.allKeys containsObject:@"group"]) {
GroupModel *model = [[GroupModel alloc] initWithDic:dic[@"group"]];
_group = model;
}else {
_group = nil;
}
if ([dic.allKeys containsObject:@"personlist"]) {
NSArray *personlist = [PersonModel getListArray:dic[@"personlist"]];
_personlist = personlist;
}else {
_personlist = nil;
}
}
return self;
}
@end
@implementation GroupModel
@end
@implementation PersonModel
@end
如果你屬性寫錯(cuò), 或者少些, 或者多寫, 進(jìn)行json解析的時(shí)候都不會(huì)引起crash