KVC取值 -- valueForKeyPath
// XMGBook.h
#import <Foundation/Foundation.h>
@interface XMGBook : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) double price;
@end
// XMGBook.m
#import "XMGBook.h"
@implementation XMGBook
@end
// main.m
#import <Foundation/Foundation.h>
#import "XMGBook.h"
int main(int argc, const char * argv[]) {
XMGBook *book1 = [[XMGBook alloc] init];
book1.name = @"123";
book1.price = 20.5;
XMGBook *book2 = [[XMGBook alloc] init];
book2.name = @"345";
book2.price = 30.5;
XMGBook *book3 = [[XMGBook alloc] init];
book3.name = @"678";
book3.price = 40.5;
NSArray *books = @[book1, book2, book3];
// 如果valueForKeyPath:方法的调用者是数组,那么就是去访问数组元素的属性值
// 取得books数组中所有XMGBook对象的name属性值,放在一个新的数组中返回
NSArray *names = [books valueForKeyPath:@"name"];
NSLog(@"%@", names);
// XMGPerson *p = [[XMGPerson alloc] init];
// p.books = @[book1, book2, book3];
//
// NSLog(@"%@", [p.books valueForKeyPath:@"@count"]);
// NSLog(@"%@", [p valueForKeyPath:@"books.@count"]);
//
// NSLog(@"%@", [p.books valueForKeyPath:@"@avg.price"]);
// NSLog(@"%@", [p valueForKeyPath:@"books.@avg.price"]);
//
// NSLog(@"%@", [p.books valueForKeyPath:@"@max.price"]);
// NSLog(@"%@", [p valueForKeyPath:@"books.@max.price"]);
//
// NSLog(@"%@", [p.books valueForKeyPath:@"@min.price"]);
// NSLog(@"%@", [p valueForKeyPath:@"books.@min.price"]);
return 0;
}
输出结果图:
// XMGPerson.h
#import <Foundation/Foundation.h>
@interface XMGPerson : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) double money;
@end
// XMGPerson.m
#import "XMGPerson.h"
@implementation XMGPerson
@end
// main.m
#import <Foundation/Foundation.h>
#import "XMGPerson.h"
int main(int argc, const char * argv[]) {
XMGPerson *person = [[XMGPerson alloc] init];
person.name = @"abc";
person.money = 9.9;
NSLog(@"%@,%@", [person valueForKeyPath:@"name"], [person valueForKeyPath:@"money"]);
return 0;
}
效果图:
KVC
- 全称:Key Value Coding(键值编码)
- 赋值
// 能够修改私有成员变量
[p setValue:<#(nullable id)#> forKey:<#(nonnull NSString *)#>];
[p setValue:<#(nullable id)#> forKeyPath:<#(nonnull NSString *)#>];
[p setValuesForKeysWithDictionary:(nonnull NSDictionary<NSString *,id> *)];
- 取值
// 能够取得私有成员变量
[p valueForKey:<#(nonnull NSString *)#>];
[p valueForKeyPath:<#(nonnull NSString *)#>];
[p dictionaryWithValuesForKeys:<#(nonnull NSArray<NSString *> *)#>];
- KVO
/*
KVO: Key Value Observing (键值监听)
作用: 监听对象的属性值发生改变, 通知给监听者
*/
#import "ViewController.h"
#import "XMGPerson.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
XMGPerson *person = [[XMGPerson alloc] init];
person.name = @"zhangsan";
// 为对象p添加一个观察者(监听器)
// Observer 观察者(监听器)
// KeyPath 键 ---> name (或者可以这样理解:属性名:需要监听哪个属性)
// options 返回给监听者旧值或者新值
[person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld context:nil];
// [person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
person.name = @"lisi";
// 释放
// [person removeObserver:self forKeyPath:@"name"];
[person removeObserver:self forKeyPath:@"name"];
}
// 应用场景很少,可能就是做彩票那里用到
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
NSLog(@"%@-----%@-----%@",keyPath, object, change);
NSLog(@"%s", __func__);
}
//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
// NSLog(@"%@-----%@-----%@",keyPath, object, change);
// if([keyPath isEqualToString:@"name"]){
// // ...业务逻辑
// }else if ([keyPath isEqualToString:@"height"]){
// // ...业务逻辑
// }
//}
KVO
全称: Key Value Observing(键值监听)
作用:监听模型的属性值改变
-
步骤
- 添加监听器
// 利用b对象来监听a对象name属性的改变 [a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld context:@"test"];
- 在监听器中实现监听方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
NSLog(@"%@-----%@-----%@------%@",keyPath, object, changem, text);
NSLog(@"%s", func);
}