字典轉模型 就是利用for in遍歷將字典中的每一個key value和模型中的屬性匹配,如何匹配?利用模型的對象調用setvaluesforkeyswithdictionary方法。注意返回值肯定是模型的類型啊(常識),如果匹配的到,就表示符合將字典轉成模型,然后將數據存進數組中保存,數組中的內容肯定是模型的類型。然后通過在cellforrowatindex 從模型中取出被indexpath.row標識的每一行數據顯示到每個cell上。模型中的有多少個數據,那么cellforrowatindex就掉用多少次。細節問題:取出被indexpath.row標識的每一行,這些行的中的數據肯定是模型類型的數據,上面有提及。然后自定義的cell對象掉用setter方法并將模型類型的對象作為參數實現設置數據到cell上。因為每調用一次cellforrowatindex就會調用自定義cell的setter方法,把某一行的數據顯示到cell上,所以數組中存儲的所有數據都會按標識的順序一次顯示到cell上
精華:從轉模型到使用數組中的模型數據的總過程為:將請求到的數據進行字典轉模型然后存進去數組,這樣以后就可以通過數組的下標拿到模型,然后可以通過模型的對象訪問屬性(實際上訪問的數組中指定數據),從而拿到數組中指定的數據了。
字典轉模型的三種做法
做法1:手動聲明和實現字典轉模型的方法,自行進行key value的匹配
#import "ViewController.h"
#import "ZB_Model.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *responseObject = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses" ofType:@"plist"]];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
// 遍歷responseObject數組中的內容,因為responseObject數組中裝的全是字典,所以利用for in遍歷出每一個dic
for (NSDictionary *dic in responseObject) {
// 打印字典中的內容
NSLog(@"%@",dic);
ZB_Model *statusesModel = [ZB_Model returnVideoModelWithDictionary:dic];
[tempArray addObject:statusesModel];
}
self.DataArray = tempArray;
NSLog(@"%@",self.DataArray);
}
@end
#import <Foundation/Foundation.h>
@interface ZB_Model : NSObject
/** 新增加的smart_img 圖片*/
@property(nonatomic,copy)NSString *smart_img;
/** 新增加的smart_img1 圖片*/
@property(nonatomic,copy)NSString *smart_img1;
/** 新增加的smart_img2 圖片*/
@property(nonatomic,copy)NSString *smart_img2;
/** 新增加的title 標題 */
@property(nonatomic,copy)NSString *title;
/** 新增加的source_name 來源名稱 */
@property(nonatomic,copy)NSString *source_name;
/** 新增加的source_ico 來源圖標 */
@property(nonatomic,copy)NSString *source_ico;
/** 新增加的type_id 1資訊2視頻 */
@property (nonatomic, assign) int type_id;
/** 新增加的sys_tag 展示標簽 1精,2熱,3廣告 */
@property(nonatomic,copy)NSString *sys_tag;
/** 新增加的url 跳轉地址 */
@property(nonatomic,copy)NSString *url;
/*** 瀏覽人數 */
@property(nonatomic, copy) NSString *viewnum;
/** 圖片*/
@property(nonatomic,copy)NSString *ImgDic3;
+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict;
@end
#import "ZB_Model.h"
@implementation ZB_Model
+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict{
ZB_Model *model = [[ZB_Model alloc] init];
// 取出dic字典中的text對應的value,放到模型的contentStr屬性中存儲
model.title = dict[@"title"];
model.viewnum = dict[@"viewnum"];
model.source_name = dict[@"source_name"];
model.ImgDic3 = dict[@"imageDic"][@"ImgDic3"];
return model;
}
@end
做法2手動聲明和實現字典轉模型的方法并調用setValuesForKeysWithDictionary:
#import "ViewController.h"
#import "ZB_Model.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *responseObject = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses" ofType:@"plist"]];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
// 遍歷responseObject數組中的內容,因為responseObject數組中裝的全是字典,所以利用for in遍歷出每一個dic
for (NSDictionary *dic in responseObject) {
// 打印字典中的內容
NSLog(@"%@",dic);
// 將字典轉成模型
ZB_Model *statusesModel = [ZB_Model returnVideoModelWithDictionary:dic];
// 模型存到臨時數組中
[tempArray addObject:statusesModel];
}
// 臨時數組賦值給全局的數組
self.DataArray = tempArray;
// 遍歷全局數組的內容。經打印可以的得知,內容是ZB_Model類型的數據
for (NSString *i in self.DataArray) {
NSLog(@"%@",i);
}
}
@end
#import <Foundation/Foundation.h>
@interface ZB_Model : NSObject
/** 新增加的smart_img 圖片*/
@property(nonatomic,copy)NSString *smart_img;
/** 新增加的smart_img1 圖片*/
@property(nonatomic,copy)NSString *smart_img1;
/** 新增加的smart_img2 圖片*/
@property(nonatomic,copy)NSString *smart_img2;
/** 新增加的title 標題 */
@property(nonatomic,copy)NSString *title;
/** 新增加的source_name 來源名稱 */
@property(nonatomic,copy)NSString *source_name;
/** 新增加的source_ico 來源圖標 */
@property(nonatomic,copy)NSString *source_ico;
/** 新增加的type_id 1資訊2視頻 */
@property (nonatomic, assign) int type_id;
/** 新增加的sys_tag 展示標簽 1精,2熱,3廣告 */
@property(nonatomic,copy)NSString *sys_tag;
/** 新增加的url 跳轉地址 */
@property(nonatomic,copy)NSString *url;
/*** 瀏覽人數 */
@property(nonatomic, copy) NSString *viewnum;
/** 圖片*/
@property(nonatomic,copy)NSString *ImgDic3;
+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict;
@end
#import "ZB_Model.h"
@implementation ZB_Model
+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict{
ZB_Model *model = [[ZB_Model alloc] init];
[model setValuesForKeysWithDictionary:dict];
return model;
}
/*
不寫下面這句代碼,會報如下的錯誤。寫了下面的代碼,即使plist中有A字段,而模型中沒有A字段也不會報錯。
'[<ZB_Model 0x6080000a4320> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageDic.'
*/
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
做法3:利用框架進行字典轉模型,不需要手動聲明和實現字典轉模型的方法
#import "ViewController.h"
#import "ZB_Model.h"
#import "MJExtension/MJExtension.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 將字典轉成模型
self.DataArray = [ZB_Model mj_objectArrayWithFilename:@"statuses.plist"];
NSLog(@"%ld",self.DataArray.count);
NSLog(@"%@",self.DataArray);
}
@end
#import <Foundation/Foundation.h>
@interface ZB_Model : NSObject
/** 新增加的smart_img 圖片*/
@property(nonatomic,copy)NSString *smart_img;
/** 新增加的smart_img1 圖片*/
@property(nonatomic,copy)NSString *smart_img1;
/** 新增加的smart_img2 圖片*/
@property(nonatomic,copy)NSString *smart_img2;
/** 新增加的title 標題 */
@property(nonatomic,copy)NSString *title;
/** 新增加的source_name 來源名稱 */
@property(nonatomic,copy)NSString *source_name;
/** 新增加的source_ico 來源圖標 */
@property(nonatomic,copy)NSString *source_ico;
/** 新增加的type_id 1資訊2視頻 */
@property (nonatomic, assign) int type_id;
/** 新增加的sys_tag 展示標簽 1精,2熱,3廣告 */
@property(nonatomic,copy)NSString *sys_tag;
/** 新增加的url 跳轉地址 */
@property(nonatomic,copy)NSString *url;
/*** 瀏覽人數 */
@property(nonatomic, copy) NSString *viewnum;
/** 圖片*/
@property(nonatomic,copy)NSString *ImgDic3;
@end
[LS](https://pan.baidu.com/s/1pKSiEZD 密碼 tmsq)