寫在前面:因為懶,不愛寫東西了旧巾。感謝我的兄弟兼iOS引路人韓韓耸序,一直監(jiān)督著我學習,逼著我理解技術(shù)和寫出來鲁猩,因此有了下文坎怪。不過大部分都是借鑒別人筆記,當然我理解后重新敲了一遍廓握,感謝萬能的簡友搅窿。
目錄
一:封裝base模型
二:字典轉(zhuǎn)模型
三:JSON字符串轉(zhuǎn)模型
四:模型中嵌套模型
五:模型中有個數(shù)組屬性,數(shù)組里面又要裝著其它模型
六:將一個字典數(shù)組轉(zhuǎn)成模型數(shù)組
0.封裝base模型
.h
@interface LccBaseModel : NSObject //模型中的屬性名和字典中的key不相同,映射替換 @property(nonatomic,copy)NSString *ID; //通過字典來創(chuàng)建一個模型 + (instancetype)objectWithDic:(NSDictionary*)dic; //通過JSON字符串轉(zhuǎn)模型 + (instancetype)objectWithJSONStr:(NSString *)jsonStr; //通過字典數(shù)組來創(chuàng)建一個模型數(shù)組 + (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. 字典轉(zhuǎn)模型
.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
/* 字典轉(zhuǎn)模型 */ - (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字符串轉(zhuǎn)模型
.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字符串轉(zhuǎn)模型 */
- (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); }
image.png
3. 模型中嵌套模型
/* 模型中嵌套模型 */ - (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); }
image.png
4.模型中有個數(shù)組屬性隙券,數(shù)組里面又要裝著其它模型
.h
#import "LccBaseModel.h" @interface StatusResult : LccBaseModel @property (strong, nonatomic) NSMutableArray *statuses; @property (strong, nonatomic) NSArray *ads; @property (strong, nonatomic) NSNumber *totalNumber; @end
/* 模型中有個數(shù)組屬性男应,數(shù)組里面又要裝著其它模型 */ - (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數(shù)組中的模型屬性 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數(shù)組中的模型屬性 for (NSDictionary *dic in model.ads) { AdsModel *ad = [AdsModel objectWithDic:dic]; NSLog(@"image=%@, url=%@", ad.image, ad.url); } }
image.png
5.將一個字典數(shù)組轉(zhuǎn)成模型數(shù)組
/* 將一個字典數(shù)組轉(zhuǎn)成模型數(shù)組 */ - (void)demo5{ NSArray *dictArray = @[ @{ @"name" : @"Jack", @"icon" : @"lufy.png", }, @{ @"name" : @"Rose", @"icon" : @"nami.png", } ]; // 將字典數(shù)組轉(zhuǎn)為User模型數(shù)組 NSArray *userArray = [UserModel objectsWithArray:dictArray]; // 打印userArray數(shù)組中的User模型屬性 for (UserModel *user in userArray) { NSLog(@"name=%@, icon=%@", user.name, user.icon); } }
image.png