Model的合理使用时机

Model:

我个人理解是,是通过一个设定的的全局变量来获取对应储存在数组里的数据。

例如:

创建模型装换

.h

#import <Foundation/Foundation.h>

 

@interface TYModel : NSObject

//在此处设定我们要装换模型的全局变量

@property (nonatomic, copy) NSString *data;

@property (nonatomic, copy) NSString *name;

+ (instancetype)addModelDic:(NSDictionary *)dic;

@end

.m

#import "TYModel.h"

//这是使用的是kvc来做为装换

@implementation TYModel

+ (instancetype)addModelDic:(NSDictionary *)dic {

    return [[selfalloc] initWithDict:dic];

}

 

- (instancetype)initWithDict:(NSDictionary *)dic {

    /*

     注意:在使用kvc是要确定传入的字典与.h中是一一对应的不然会出现崩溃

     */

    // 使用KVC 字典转模型如此方便,省去了大量的赋值代码

    if (self = [selfinit]) {

        [selfsetValuesForKeysWithDictionary:dic];

    }

    returnself;

}

@end

将我们从接口那里获取过来的数据进行转换,在这里我就字典里的数据插入到数组中模拟接口数据

#import "ViewController.h"

#import "TYModel.h"

@interfaceViewController ()

@property (nonatomic, strong) NSMutableArray *modelMutableArray;

@end

 

@implementation ViewController

//可变数组懒加载

- (NSMutableArray *)modelMutableArray {

    if (!_modelMutableArray) {

        _modelMutableArray = [NSMutableArrayarray];

    }

    return_modelMutableArray;

}

 

- (void)viewDidLoad {

    [superviewDidLoad];

    NSDictionary * dic = @{@"data":@"123",@"name":@"小花"};

    NSDictionary * dic1 = @{@"data":@"321",@"name":@"小立"};

    NSArray *array = [NSArrayarrayWithObjects:dic,dic1,nil];

    //遍历数组取出字典放入模型中进行转换,装换成功后放入数组中储存起来

    for (NSDictionary *modelDic in array) {

        TYModel *model = [TYModeladdModelDic:modelDic];

        [self.modelMutableArrayaddObject:model];

    }

    //提取我们转换出来的模型

    TYModel *mo = _modelMutableArray[0];

    NSLog(@"模型是否成功:%@",mo.name);

}

 

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

打印的结果:

2015-12-2614:49:32.535 model[1151:33138] 模型是否成功:小花

 

看起来是不是简单、好用,高效率呢!的确如果我们的接口如传入过来的数据中有多个数组,数组里的字典字段也非常多。我们使用这种模型是非常好用的,且也不会出现什么错误。然而当我们在某个页面中使用的字段不是很多,就一个数组,这时我们有必要在这里创建一个模型吗?

例如:

#import "ViewController.h"

#import "TYModel.h"

#define Height[UIScreen mainScreen].bounds.size.height

#define Width[UIScreen mainScreen].bounds.size.width

@interfaceViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *modelMutableArray;

@property (nonatomic, strong) NSDictionary *dataDic;

@property (nonatomic, strong) NSArray *dicArray;

@end

 

@implementation ViewController

//可变数组懒加载

- (NSMutableArray *)modelMutableArray {

    if (!_modelMutableArray) {

        _modelMutableArray = [NSMutableArrayarray];

    }

    return_modelMutableArray;

}

 

- (void)viewDidLoad {

    [superviewDidLoad];

//    NSDictionary * dic =@{@"data":@"123",@"name":@"小花"};

//    NSDictionary * dic1 =@{@"data":@"321",@"name":@"小立"};

//    NSArray *array = [NSArrayarrayWithObjects:dic,dic1,nil];

    //遍历数组取出字典放入模型中进行转换,装换成功后放入数组中储存起来

//    for (NSDictionary *modelDic inarray) {

//        TYModel *model = [TYModeladdModelDic:modelDic];

//        [self.modelMutableArrayaddObject:model];

//    }

    //提取我们转换出来的模型

//    TYModel *mo = _modelMutableArray[0];

    NSDictionary * dic = @{@"data":@"123",@"name":@"小花",@"position":@"iOS开发",@"level":@"开发主管",@"age":@"27"};

    NSArray *array = [NSArrayarrayWithObjects:dic,nil];

    _dataDic = array[0];

    NSLog(@"模型是否成功:%@",_dataDic[@"name"]);

    _dicArray = [NSArrayarrayWithObjects:_dataDic[@"data"],_dataDic[@"name"],_dataDic[@"position"],_dataDic[@"level"],_dataDic[@"age"],nil];

  

    [selfaddTableView];

}

 

- (void)addTableView {

    UITableView *tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStylePlain];

    tableView.delegate = self;

    tableView.dataSource = self;

    [self.viewaddSubview:tableView];

}

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return1;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {

        return_dicArray.count;

    }

    return0;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化

    staticNSString *cellIdentifier=@"cell";

    //首先根据标识去缓存池取

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //如果缓存池没有到则重新创建并放到缓存池中

    if(!cell){

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:cellIdentifier];

    }

    cell.textLabel.text = _dicArray[[indexPath row]];

    return cell;

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

在这里是不可能进行转模型的,因为要达到我们的设计效果我们后台给出的数据达不到转模型的条件。如果非要转模型,那就比较复杂了得不偿失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值