写在前面:因为懒,不爱写东西了。感谢我的兄弟兼iOS引路人韩韩,一直监督着我学习,逼着我理解技术和写出来,因此有了下文。不过大部分都是借鉴别人笔记,当然我理解后重新敲了一遍,感谢万能的简友。
目录
一:封装base模型
二:字典转模型
三:JSON字符串转模型
四:模型中嵌套模型
五:模型中有个数组属性,数组里面又要装着其它模型
六:将一个字典数组转成模型数组
0.封装base模型
.h
@interface LccBaseModel : NSObject //模型中的属性名和字典中的key不相同,映射替换 @property(nonatomic,copy)NSString *ID; //通过字典来创建一个模型 + (instancetype)objectWithDic:(NSDictionary*)dic; //通过JSON字符串转模型 + (instancetype)objectWithJSONStr:(NSString *)jsonStr; //通过字典数组来创建一个模型数组 + (NSArray*)objectsWithArray:(NSArray<NSDictionary*>*)arr; @end
.m
#import "LccBaseModel.h" #import "MJExtension.h" @implementation LccBaseModel + (NSDictionary *)mj_replacedKeyFromPropertyName{ // 替换key return @{@"ID":@"id"}; } + (instancetype)objectWithDic:(NSDictionary*)dic{ > //容错处理 if (![dic isKindOfClass:[NSDictionary class]]||!dic) { return nil; } NSString *className = [NSString stringWithUTF8String:object_getClassName(self)]; return [NSClassFromString(className) mj_objectWithKeyValues:dic]; } + (instancetype)objectWithJSONStr:(NSString *)jsonStr{ //容错处理 if (![jsonStr isKindOfClass:[NSString class]]||!jsonStr) { return nil; } NSString *className = [NSString stringWithUTF8String:object_getClassName(self)]; return [NSClassFromString(className) mj_objectWithKeyValues:jsonStr]; } + (NSArray*)objectsWithArray:(NSArray<NSDictionary*>*)arr{ //获取子类名 NSString * className = [NSString stringWithUTF8String:object_getClassName(self)]; return [NSClassFromString(className) mj_objectArrayWithKeyValuesArray:arr]; } @end
1. 字典转模型
.h
UserModel模型 #import "LccBaseModel.h" typedef enum { SexMale, SexFemale } Sex; @interface UserModel : LccBaseModel @property (nonatomic,copy) NSString *name; @property (nonatomic,assign) Sex sex; @property (nonatomic,assign) NSInteger age; @property (nonatomic,copy) NSDictionary *dataDic; @property (nonatomic,copy) NSString *msg; @property (nonatomic,copy) NSString *icon;
@end
/* 字典转模型 */ - (void)demo1{ NSDictionary *dic = @{@"id":@"111111", @"name":@"韩韩", @"age":@18, @"sex": @(SexFemale) }; UserModel *model = [UserModel objectWithDic:dic]; NSLog(@"dic = %@ \n id = %@,name = %@,age:%zd,sex:%u",dic,model.ID,model.name,model.age,model.sex);
}
log.png
2. JSON字符串转模型
.h
ShopModel模型 #import "LccBaseModel.h" @class UserModel; @interface ShopModel : LccBaseModel @property (nonatomic,copy) NSString *productName; @property (nonatomic,assign) NSInteger price; @property (nonatomic,strong) UserModel *user; @end
/* JSON字符串转模型 */
image.png- (void)demo2{ NSString *jsonStr = @"{\"id\":\"111111\",\"name\":\"韩韩\", \"age\":18}"; UserModel *model = [UserModel objectWithJSONStr:jsonStr]; NSLog(@"jsonStr = %@ \n id = %@,name = %@,age:%zd",jsonStr,model.ID,model.name,model.age); }
3. 模型中嵌套模型
image.png/* 模型中嵌套模型 */ - (void)demo3{ NSDictionary *dic = @{ @"msg":@"成功", @"dataDic":@{ @"productName":@"飞机杯", @"price":@"500", @"user":@{ @"name":@"韩大帅哥", @"age":@18, } }, }; UserModel *model = [UserModel objectWithDic:dic]; ShopModel *shopModel = [ShopModel objectWithDic:model.dataDic]; NSLog(@"msg= %@",model.msg); NSLog(@"name= %@ age:%ld",shopModel.user.name,(long)shopModel.user.age); NSLog(@"productName= %@ price= %ld",shopModel.productName,(long)shopModel.price); }
4.模型中有个数组属性,数组里面又要装着其它模型
.h
#import "LccBaseModel.h" @interface StatusResult : LccBaseModel @property (strong, nonatomic) NSMutableArray *statuses; @property (strong, nonatomic) NSArray *ads; @property (strong, nonatomic) NSNumber *totalNumber; @end
image.png/* 模型中有个数组属性,数组里面又要装着其它模型 */ - (void)demo4{ NSDictionary *dict = @{ @"statuses" : @[ @{ @"text" : @"今天天气真不错!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } }, @{ @"text" : @"明天去旅游了", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" } } ], @"ads" :@[ @{ @"image" : @"ad01.png", @"url" : @"http://www.ad01.com" }, @{ @"image" : @"ad02.png", @"url" : @"http://www.ad02.com" } ], @"totalNumber" : @"2014" }; NSLog(@"dict= %@",dict); StatusResult *model = [StatusResult objectWithDic:dict]; NSArray *dataArray = [StatusResult objectsWithArray:model.statuses]; NSLog(@"dataArray= %@",dataArray); // 打印statuses数组中的模型属性 for (NSDictionary *dic in model.statuses) { StatusModel *status = [StatusModel objectWithDic:dic]; NSString *text = status.text; NSString *name = status.user.name; NSString *icon = status.user.icon; NSLog(@"text=%@, name=%@, icon=%@", text, name, icon); } // 打印ads数组中的模型属性 for (NSDictionary *dic in model.ads) { AdsModel *ad = [AdsModel objectWithDic:dic]; NSLog(@"image=%@, url=%@", ad.image, ad.url); } }
5.将一个字典数组转成模型数组
image.png/* 将一个字典数组转成模型数组 */ - (void)demo5{ NSArray *dictArray = @[ @{ @"name" : @"Jack", @"icon" : @"lufy.png", }, @{ @"name" : @"Rose", @"icon" : @"nami.png", } ]; // 将字典数组转为User模型数组 NSArray *userArray = [UserModel objectsWithArray:dictArray]; // 打印userArray数组中的User模型属性 for (UserModel *user in userArray) { NSLog(@"name=%@, icon=%@", user.name, user.icon); } }
网友评论