KVO/KVC 中set方法

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];

posted on 2015-03-16 14:27  香草丶  阅读(101)  评论(0)    收藏  举报