首页 > 移动平台 > 详细

iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:

时间:2016-09-07 12:39:03      阅读:374      评论:0      收藏:0      [点我收藏+]

这是一个小坑, 看看你的BaseModel的便利构造器的方法:

+ (__kindof BaseModel *)modelWithDic:(NSDictionary *)dic {
    return [[self alloc] initWithDic:dic];
}

self 一定要写成self, 这样才会调用子类的setvalue的方法. 不要写成BaseModel. 

 

BaseModel.m中的代码如下:

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}

+ (__kindof BaseModel *)modelWithDic:(NSDictionary *)dic {
    return [[self alloc] initWithDic:dic];
}

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

- (id)valueForUndefinedKey:(NSString *)key {
    return nil;
}

 

iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:

原文:http://www.cnblogs.com/mafeng/p/5848746.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!