首页 > 其他 > 详细

KVO/KVC 中set方法

时间:2015-03-16 15:49:16      阅读:395      评论:0      收藏:0      [点我收藏+]
1.使用setValue:ForKey:设置Student对象的name

[student setValue:@"one" forKey:@"name"];

 

 

     使用setValue:ForKey:设置Student对象的age

[student setValue:[NSNumber numberWithInt:17] forKey:@"age"];

 在设置一个标量值时,需要先将标量值包装成一个NSNumber或NSValue对象

 

 

       2.KVC可以对对象进行批量更改
         例如,同时获取Student的age和name

NSArray *keys = [NSArray arrayWithObjects:@"name", @"age", nil];

NSDictionary *dict = [student dictionaryWithValuesForKeys:keys];

 

    3.  同时设置Student的age和name

NSArray *keys = [NSArray arrayWithObjects:@"name", @"age", nil];

NSArray *values = [NSArray arrayWithObjects:@"One", [NSNumber numberWithInt:16], nil];

NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];

[student setValuesForKeysWithDictionary:dict];

KVO/KVC 中set方法

原文:http://www.cnblogs.com/Lidear/p/4341765.html

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