字典转模型->把每一个模型存到数组->用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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,904评论 0 4
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,468评论 30 472
  • 返京。 省了早起,也省了没带身份证的麻烦,前一天试着约车,约到的是邯郸过来的顺风车,司机绕了很远的路,半天才接到我...
    还好你来了阅读 2,481评论 0 0
  • 一天下午,在闺蜜家喝下午茶。 男孩们挤到一个房间里,玩电脑游戏。 妈妈们坐在餐桌旁,吃橘子,聊怎么养多肉。 闺蜜的...
    唐砚阅读 6,828评论 24 102
  • 首先,对你说声抱歉,因为下载简书申请账号后,一直搁置在手机里面了,对这个app不怎么熟悉,昨天打开无意中日刷到那篇...
    cindyyyy阅读 1,630评论 0 1

友情链接更多精彩内容