转载

iOS开发UI之KVC(取值/赋值) - KVO (观察某个对象的某个属性的改变)

一. KVC : key value coding,通常用来给某一个对象的属性赋值

1. KVC赋值

// 1.1 创建人 LDPerson *p = [[LDPerson alloc] init]; self.person = p; // 1.2 创建狗 LDDog *dog = [[LDDog alloc] init]; // 1.3 将狗赋值给人 [p setValue:dog forKeyPath:@"dog"]; // 1.4 通过kvc给dog的weight属性赋值 / 赋值时会自动找到人拥有的dog的weight属性 [p setValue:@20.0 forKeyPath:@"dog.weight"]; NSLog(@"books = %@", [p valueForKeyPath:@"dog.weight"]); [dog print];

2. KVC字典赋值

// 2.1 创建一个字典,person的属性为键,对应赋值 NSDictionary *dict = @{@"name": @"jack", @"age": @"10", @"height": @"1.65"}; // 2.2 通过字典中的键找到person对象属性进行赋值 [p setValuesForKeysWithDictionary:dict]; NSLog(@"name = %@, age = %@, height = %@", [p valueForKeyPath:@"name"], [p valueForKeyPath:@"age"], [p valueForKeyPath:@"height"]);

3. 自定义字典

// 3.1 定义字典 NSDictionary *dict1 = @{@"name": @"jim",  @"age": @"20",  @"height": @"1.75",  @"books": @[@{@"price": @"100"}, @{@"price": @"98"}, @{@"price": @"200"}, @{@"price": @"198"}], @"dog": @{@"weight": @"45.89"}}; // 3.2 kvc赋值 [p setValuesForKeysWithDictionary:dict1]; // 3.3 输出boos中存放的是字典 NSLog(@"books = %@", [p valueForKeyPath:@"books"]); NSLog(@"dog.weight = %@", [p valueForKeyPath:@"dog.weight"]); // 3.4 遍历,让books中存放字典 NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in [p valueForKeyPath:@"books"]) {     LDBook *book = [LDBook bookWithDict:dict];     [arrayM addObject:book]; } [p setValue:arrayM forKeyPath:@"books"]; NSLog(@"books = %@", [p valueForKeyPath:@"books"]); 

4. KVC取值

NSMutableArray *tempM = [NSMutableArray array]; // 4.1 kvc取出出数组books中price的值 for (LDBook *book in [p valueForKeyPath:@"books"]) {  [tempM addObject:[book valueForKeyPath:@"price"]]; } NSLog(@"%@", tempM); // 4.2 kvc取出数组中price的最大值/最小值/平均值/个数 NSLog(@"Max = %@", [[p valueForKeyPath:@"books"] valueForKeyPath:@"@max.price"]); NSLog(@"Min = %@", [[p valueForKeyPath:@"books"] valueForKeyPath:@"@min.price"]); NSLog(@"Avg = %@", [[p valueForKeyPath:@"books"] valueForKeyPath:@"@avg.price"]); NSLog(@"count = %@", [[p valueForKeyPath:@"books"] valueForKeyPath:@"@count.price"]); 

二. KVO : Key Value OBserver (观察者)

通过KVO可以观察某一个对象的属性值发生改变

1. 给Person创建一个观察者,观察Person的name属性

[p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

2. 修改Person的name的值

[p setValue:@"ldd" forKey:@"name"];

3. 当Person的name的值发生改变时,会回调

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

#pragma mark - 实现KVO回调方法 /**  *  当对象的属性发生改变会调用该方法  *  *  @param keyPath 监听的属性  *  @param object  监听的对象  *  @param change  新值和旧值  *  @param context 额外的数据  */ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {     NSLog(@"KeyPath = %@", keyPath);     NSLog(@"object = %@", object);     NSLog(@"change = %@", change); }

4. 当回调完成之后销毁观察者

/**  *  销毁Person的观察者  */ - (void)dealloc {     [self.person removeObserver:self forKeyPath:@"name" context:nil]; }
正文到此结束
Loading...