您的位置:首页 > 其它

KVC一句话搞定赋值

2015-11-19 10:22 302 查看
- (instancetype)initWithDic:(NSDictionary*)dic{

if (self = [super init]) {

[self setValuesForKeysWithDictionary:dic];
}
return self;
}
如果没有哪个属性,自己定义的而服务器没有这个属性的!
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

if ([key isEqualToString:@"comment"]) {
NSInteger commentCount = [value integerValue];
_pinLun = [[NSString stringWithFormat:@"%ld",(long)commentCount] copy];
}
NSLog(@"%@",value);
}

comment 是服务器的属性
_pinlun 是自己定义的属性


- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

NSLog(@"=------:%@",key);

if ([key isEqualToString:@"description"]) {
_des = value;
}else if ([key isEqualToString:@"photos"]){

NSArray *photos = (NSArray*)value;
NSMutableArray *appPhotoes = [NSMutableArray array];
[photos enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
//            1.创建AppPhoto对象
AppPhoto *photo = [AppPhoto appPhotoWithDic:dic];
//            2.将上面的对象添加到appPhotoes中
[appPhotoes addObject:photo];

}];
_appPhotoes = appPhotoes;

}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: