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