UI基础-day03-Shopping-06懒加载-3字典转模型

UI基础-day02-Shopping-01搭建商城基本框架
UI基础-day02-Shopping-02添加一个商品
UI基础-day02-Shopping-03九宫格排序
UI基础-day02-Shopping-04优化添加和删除功能
UI基础-day02-Shopping-05加载数据
UI基础-day02-Shopping-06懒加载-1基础
UI基础-day03-Shopping-06懒加载-2Plist文件读取

分析

  1. 通过NSString的Key取数据,拼写错误时.既不会报错,也没有警告.很难定位错误. 如: 在shopDataArray里,元素是icon, 非ico.
NSDictionary *dicShop = self.shopDataArray[shopIndex];
UIImage * imgTemp = [UIImage imageNamed:dicShop[@"ico"]];
  1. ViewController做了太多事情了.

字典转模型

新建模型

@interface HUHShopModel : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;

字典转模型(shops集合里面装的都是HUHShopModel)

@property (nonatomic, strong) NSMutableArray *shops;

-(NSMutableArray *)shops{
    if (_shops == nil) {
        _shops = [NSMutableArray array];
        NSString *path = [[NSBundle mainBundle ]pathForResource:@"文件名" ofType:@"plist"];
        NSArray *shopData = [NSArray arrayWithContentsOfFile:path];// 获取Plist文件的数据
        
        //遍历每个字典
        for (NSDictionary* dic in shopData) {
            HUHShopModel * shopM = [[HUHShopModel alloc]init];
            //字典-->模型
            shopM.name = dic[@"name"];
            shopM.icon = dic[@"icon"];
            [_shops addObject:shopM];
        }
    }
    return _shops;
}

取出集合里指定的元素,通过类型.属性的方法,实现出错有提示!

HUHShopModel * shopM = self.shops[索引];
UIImage * imgTemp = [UIImage imageNamed:shopM.icon];

封装字典转模型

新建模型

@interface HUHShopModel : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
+(instancetype)shopsWithDict:(NSDictionary *)dict;
-(instancetype)initWithDict:(NSDictionary *)dict;

封装'字典转模型'

@implementation HUHShopModel

+(instancetype)shopsWithDict:(NSDictionary *)dict{
    return  [[self alloc]initWithDict:dict];
}

-(instancetype)initWithDict:(NSDictionary *)dict{
    if(self = [super init]){ //调用父类构造方法
        self.name = dict[@"name"];
        self.icon = dict[@"icon"];
    }
    return self;
}

注意: 为什么不能写[[类名 alloc]initWithDict:dict]; 要写
[[self alloc]initWithDict:dict];
举例:
想要的是子类XMGGoodShop,但是子类调用父类方法,而父类固定了是,XMGShopModel


@property (nonatomic, strong) NSArray *shopDataArray;

-(NSArray *)shopDataArray{
    if (_shopDataArray == nil) {
       ......
        NSMutableArray *shopTemp = [NSMutableArray array];//临时可变数组
        NSArray *shopData = [NSArray arrayWithContentsOfFile:path];
        //遍历每个字典
        for (NSDictionary* dict in shopData) {
           //封装字典-->模型, 使用类方法
            HUHShopModel * shopM = [HUHShopModel shopsWithDict:dict];
            [shopTemp addObject:shopM];
        }
        _shopDataArray = shopTemp;//直接赋值给不可变数组
    }
    return _shopDataArray;
}

注意: 为什么不直接使用可变数组,而一定要使用不可变数组?

  1. 采用可变数组,在外部可以直接删除! 如下:


  2. 采取方法是:
    1.内部采用临时可变数组,来存放已经转换为模型的数据.
    2.最后将可变数组传给不可变数组.

分析错误

为什么是102行报错,不是101行报错呢?


  1. 从编译器角度,它并不知道,数组里面装的是什么类型!
    所以它返回的是ID类型.用ShopModel来接收,不会报错.
    101行不报错.
  2. 但是,程序的运行过程中,会动态的检测对象的真实类型(动态绑定).
    self.shops[index]返回的是字典.
    shop.icon= get方法, 没有此方法.(字典没有icon()方法)
    所以,102行报错

你可能感兴趣的:(UI基础-day03-Shopping-06懒加载-3字典转模型)