UI基础(二)之懒加载和字典转模型

问题1:什么是懒加载?什么是字典转模型?模型又是什么?为什么要用字典转模型?为什么要写懒加载?self.属性和_属性的使用注意?为什么控件要用weak? string要用copy?

懒加载:

懒加载也成为延迟加载,只有在需要加载的时候才去加载,其实就是重写属性的getter方法,那么我们为什么要懒加载呢?

本质上讲:懒加载就是需要加载数据的时候就用,不需要就不用,为什么这么讲?

如下例:懒加载(本质就是重写get方法)(重写get方法本质就是获取值),实现协议方法的时候,return self.dataArray.count;调用了get方法,即调用了懒加载的方法,

这个时候我是需要数据的时候,那我就获取数据,但如果我不需要获取数据,即我不去调用懒加载的方法,我就不会去加载数据,消耗内存,这就是懒加载的好处,需要加载数据

就获取,不需要就不获取.

 

注意点:还有些人会问到,为什么要加个if判断在里面,这是因为,如果不加if判断,每一次调用懒加载这个get方法时,就会重新从plist文件中加载数据,这样是很耗性能的,加个if判断后,加载一次即可,此时字典数组中就有值了,下次再调用就不会走if判断中的语句了,直接就返回给我们一个_dataArray,不是很神奇么.

 

#import "CWViewController.h"
#import "CWCarModel.h"
//设置数据源方法
@interface CWViewController ()<UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataArray;
@end

@implementation CWViewController
//懒加载
- (NSArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cars" ofType:@"plist"]];
        //字典转模型
        //1.创建可变数组
        NSMutableArray *nmArray = [NSMutableArray array];
        //2.遍历字典数组
        for (NSDictionary *dict in _dataArray) {
            //字典转模型
            CWCarModel *model = [CWCarModel carsWithDict:dict];
            //将模型添加到可变数组
            [nmArray addObject:model];
            
        }
        //将可变数组中的模型赋值给字典数组
        _dataArray = nmArray;
        
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.dataArray;
    
    [self setUpUI];
}
- (void)setUpUI {
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
#pragma mark -- 以上是做了数据处理,把要访问的数组存储到了字典数组中,下面是为了把数据显示出来
    //设置代理对象
    self.tableView.dataSource = self;
    
}
#pragma mark -- 实现协议方法
//返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//调用了dataArray 的getter方法
return self.dataArray.count; } //每组有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //获取模型对象 CWCarModel *model = self.dataArray[section]; return model.cars.count; } //每行显示什么内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //实例化控件 UITableViewCell *cell = [[UITableViewCell alloc]init]; //1.获取模型对象 CWCarModel *model = self.dataArray[indexPath.section]; //2.获取汽车名 NSString *str = model.cars[indexPath.row]; //3.设置文字信息 cell.textLabel.text = str; return cell; } #pragma mark -- 返回头尾组标题 //组头 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { CWCarModel *model = self.dataArray[section]; return model.title; } //组尾 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { CWCarModel *model = self.dataArray[section]; return model.desc; } @end

 如果我没有用懒加载,如下例:

@interface ViewController ()

@property (nonatomic, strong) NSArray *shopData;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _shopData = [NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]];
}

@end

我直接在viewDidLoad{}方法里加载数据,也就是说我每次加载控制器完成后就获取一次数据,不管我会不会用到数据都要加载他,那么造成的结果就是,

如果这个数据我一直不用,而这个数据又很庞大,明显用缓存不太合适, 因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,

我们让数据在需要的时候才进行加载,就会好很多.

字典转模型:

问题:为什么要用模型?我就用字典不行么?

如果我们在plist文件中直接通过键名获取数据信息,

在viewController中需要直接和数据打交道,如果需要多次使用可能会因为不小心把键名写错,而程序并不报错。鉴于此,可以考虑把字典数据转换成一个模型,把数据封装到一个模型中去,让viewController不再直接和数据打交道,而是和模型交互。

一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编辑器没有智能提示,需要手敲。如:

dict[@"name"] = @"Jack";

NSString *name = dict[@"name"];

手敲字符串key,key容易写错

Key如果写错了,编译器不会有任何警告和报错,造成设错数据或者取错数据

字典转模型的好处:

(1)降低代码的耦合度

(2)所有字典转模型部分的代码统一集中在一处处理,降低代码出错的几率

(3)在程序中直接使用模型的属性操作,提高编码效率 

(4)调用方不用关心模型内部的任何处理细节

所以,模型是对字典数据的一种封装和优化.

 

posted @ 2016-10-14 23:29  忆缘晨风  阅读(302)  评论(0编辑  收藏  举报