iOS_KVC与KVO
目 录:
一、KVC
二、KVO
一、KVC机制
1、KVC简述
KVC是KeyValueCoding(键值对的编码)的简称,它是一种可以直接通过字符串的名字(key)来访问类属性(实例变量)的机制。 而不是通过调用Setter、Getter方法访问。KVO 就是基于 KVC 实现的关键技术之一。
2,使用方法
关键方法定义在:NSKeyValueCodingprotocol。
KVC支持类对象和内建基本数据类型。
KVC的常用方法:
- (id)valueForKey:(NSString *)key;//获取
-(void)setValue:(id)value forKey:(NSString *)key;//设置
valueForKey的方法根据key的值读取对象的属性,setValue:forKey:是根据key的值来写对象的属性。
例如:
[person setValue:@“Tom” forKey:@“name”];
NSString *name = [person valueForKey:@“name”];
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_name; NSInteger _age; Person *_wife; } @end
#import "ViewController.h" #import "Person.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建对象 Person *person = [[Person alloc]init]; //使用KVC来存储对象的数据成员 [person setValue:@"Tom" forKey:@"_name"]; [person setValue:@25 forKey:@"_age"]; NSLog(@"person:%@",person); //取对象的数据成员 NSString *name = [person valueForKey:@"_name"]; NSInteger age = [[person valueForKey:@"_age"]integerValue]; NSLog(@"KVC方式存取:name:%@,age:%ld",name,age); } @end
如果需要打印输出对象,还需要重写Person类的descaprition方法。
#import "Person.h" @implementation Person -(NSString *)description { return [NSString stringWithFormat:@"name:%@,age:%ld",_name,_age]; } @end
3、扩展KVC的使用
Book类的.h文件 #import <Foundation/Foundation.h> @interface Book : NSObject @property(copy,nonatomic)NSString *bookName; @end Book类的.m文件 #import "Book.h" @implementation Book -(NSString *)description { return [NSString stringWithFormat:@"%@",_bookName]; } @end
Person类的.h文件 #import <Foundation/Foundation.h> @class Book; @interface Person : NSObject @property(copy,nonatomic)NSString *name; @property(assign,nonatomic)NSInteger age; @property(strong,nonatomic)Book *book; @end Person类的.m文件 #import "Person.h" @implementation Person -(NSString *)description { return [NSString stringWithFormat:@"name:%@,age:%ld",_name,_age]; } @end
最后是实现文件。
#import "ViewController.h" #import "Book.h" #import "Person.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Person *p1 = [[Person alloc]init]; [p1 setValue:@"Tom" forKey:@"name"]; [p1 setValue:@23 forKey:@"age"]; Book *book1 = [[Book alloc]init]; book1.bookName = @"iPhone"; p1.book = book1; Person *p2 = [[Person alloc]init]; [p2 setValue:@"Jerry" forKey:@"name"]; [p2 setValue:@23 forKey:@"age"]; Book *book2 = [[Book alloc]init]; book2.bookName = @"iOS"; p2.book = book2; NSLog(@"%@%@",p1,[p2 valueForKeyPath:@"name"]); NSArray *person = @[p1,p2]; NSLog(@"%@",person); NSArray *arrayM = [person valueForKeyPath:@"book.bookName"]; NSLog(@"%@",arrayM); } @end
二、KVO机制
1、KVO简述
KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。
2、使用方法
系统框架已经支持KVO,所以 程序员在使用的时候非常简单。使用步骤:
(1)注册需要观察的对象的属性addObserver:forKeyPath:options:context:
(2)实现observeValueForKeyPath:ofObject:change:context:方法,这个方法当观察的属性变化时会自动调用
(3)取消注册观察removeObserver:forKeyPath:context:
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_name; } @end
#import "Person.h" @implementation Person //接受被观察者发生变动后的通知 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"%@,%@,%@,%@",keyPath,object,change,context); } @end
再添加一个股票类,让Person作为观察者,对股票的价格进行监听。
#import <Foundation/Foundation.h> @interface Stock : NSObject { NSString *_name; float _price; } @end
实现文件内容:
#import "ViewController.h" #import "Person.h" #import "Stock.h" @interface ViewController () @property(strong,nonatomic)Person *person; @property(strong,nonatomic)Stock *stock; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建观察者 self.person = [[Person alloc]init]; [self.person setValue:@"Tom" forKey:@"_name"]; //创建股票 self.stock = [[Stock alloc]init]; [self.stock setValue:@"sxt" forKey:@"_name"]; [self.stock setValue:@1.2 forKey:@"_price"]; //设置观察者 [self.stock addObserver:self.person forKeyPath:@"_price" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:@"add"]; } //单击按钮让股票价格加1 - (IBAction)buttonClicked:(UIButton *)sender { float price = [[self.stock valueForKey:@"_price"]floatValue]; [self.stock setValue:@(price+1.0) forKey:@"_price"]; } -(void)dealloc { //删除观察者 [self.stock removeObserver:self.person forKeyPath:@"_price"]; } @end