字典轉模型->把每一個模型存到數組->用indexPath訪問數組的每一個對象并顯示到cell上

字典轉模型 就是利用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)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 第5章 引用類型(返回首頁) 本章內容 使用對象 創建并操作數組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,272評論 0 4
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,217評論 30 472
  • 返京。 省了早起,也省了沒帶身份證的麻煩,前一天試著約車,約到的是邯鄲過來的順風車,司機繞了很遠的路,半天才接到我...
    還好你來了閱讀 308評論 0 0
  • 一天下午,在閨蜜家喝下午茶。 男孩們擠到一個房間里,玩電腦游戲。 媽媽們坐在餐桌旁,吃橘子,聊怎么養多肉。 閨蜜的...
    唐硯閱讀 2,345評論 24 102
  • 首先,對你說聲抱歉,因為下載簡書申請賬號后,一直擱置在手機里面了,對這個app不怎么熟悉,昨天打開無意中日刷到那篇...
    cindyyyy閱讀 225評論 0 1