zoukankan      html  css  js  c++  java
  • 字典转模型 重写初始化方法

    对 字典转模型 重写初始化方法 这两点的原理理解的不透彻,
    但是不能太较真,拖慢学习进度,先记录下,以后或许可以比较容易理解.

    #pragma mark  懒加载数据
    - (NSArray *)dataArray {
        if (nil == _dataArray) {
            
            // 1. 获取文件路径
            NSString *path = [[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil];
            
            // 2. 读取文件内容到临时数组
            NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
            
            // 3. 创建一个可变数组
            NSMutableArray *mutable = [NSMutableArray array];
            
            // 4. 遍历临时数组, 字典转模型
            for (NSDictionary *dict in tempArray) {
                GuessModel *model = [GuessModel guessModelWithDict:dict];
                
                // 添加到可变数组中
                [mutable addObject:model];
            }
            
            // 5. 把可变数组赋值给 _dataArray
            _dataArray = mutable;
            
        }
        return _dataArray;
    }
    
    #import "GuessModel.h"
    
    @implementation GuessModel
    
    - (instancetype)initWithDict:(NSDictionary *)dict {
        if (self = [super init]) {
            
            [self setValuesForKeysWithDictionary:dict];
        }
        return self;
    }
    
    + (instancetype)guessModelWithDict:(NSDictionary *)dict {
        return [[self alloc] initWithDict:dict];
    }
    
    @end
    
  • 相关阅读:
    时间选择框(可用于Form)
    点击复制指定内容
    ajax中多个模板之间套用ajax
    Java学习路径
    Windows平台安装Python
    Python语法-第2关
    Python语法-第1关
    Python语法-第0关
    图像识别
    wx:for用法
  • 原文地址:https://www.cnblogs.com/aiti/p/5155377.html
Copyright © 2011-2022 走看看