IOS中级篇 —— 字典转模型
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
-(instancetype) initWithDic:(NSDictionary *)dic;
@property (nonatomic, copy) NSString *name;
-(instancetype) initWithDic:(NSDictionary *)dic;
+(instancetype) appViewWithDic:(NSDictionary *)dic;
-(instancetype)initWithDic:(NSDictionary *)dic{
if ([super init]) {
_icon = dic[@"icon"];
_name = dic[@"name"];
}
return self;
}
+(instancetype)appViewWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
_name = dic[@"name"];
}
return self;
}
+(instancetype)appViewWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
-(NSArray *)apps{
if (!_apps) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mArray = [NSMutableArray array];
for (NSDictionary *dic in array) {
appViewModel *model = [appViewModel appViewWithDic:dic];
[mArray addObject:model];
}
_apps = mArray;
}
return _apps;
if (!_apps) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mArray = [NSMutableArray array];
for (NSDictionary *dic in array) {
appViewModel *model = [appViewModel appViewWithDic:dic];
[mArray addObject:model];
}
_apps = mArray;
}
return _apps;
}
如果在外部用来模型来做为成员变量 一定要重新模型变量的set方法 否则没值