KVC方法快速为数据对象赋值

来源:互联网 发布:json rpc restful 编辑:程序博客网 时间:2024/06/05 11:19
利用KVC方法快速为数据对象赋值,如下例

在.h文件中声明如下形式方法:


- (id)initWithDic:(NSDictionary *)dic;

在对应.m文件中

- (id)initWithDic:(NSDictionary *)dic

{   

     self = [superinit];   

     if (self)

{       

          [selfsetValuesForKeysWithDictionary:dic];   

}   

      returnself;

}


同时实现下面方法 ,实现内容一般不写(必须实现,否则无法使用KVC)
- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{   

}

0 0
原创粉丝点击