您的位置:首页 > 其它

KVC/KVO简单用法

2016-03-08 10:41 211 查看
1.0 KVC(Key-Value-Coding) :键值编码。通过某些方法,访问类的属性。

创建一个继承于NSObject的Person类

Person.h
<span style="color:#00afca;">#import <Foundation/Foundation.h>

@interface Person : NSObject
</span><p style="color: rgb(255, 255, 255); margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; min-height: 28px;"><span style="font-size:12px;">
</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:12px;color:#33ffff;background-color: rgb(204, 204, 204);">{</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:12px;color:#33ffff;background-color: rgb(204, 204, 204);">    NSString *name;</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:12px;color:#33ffff;background-color: rgb(204, 204, 204);">}</span></p><div style="color: rgb(0, 175, 202);"><span style="font-size:12px;">
</span></div><span style="color:#00afca;">
@end
</span>
</pre><span style="color:#00afca">Person.m</span><pre name="code" class="objc" style="color: rgb(0, 175, 202);">#import "Person.h"

@implementation Person

@end
并且对name属性进行检测

#import "ViewController.h"
#import "Person.h"
#import "PersonMonitor.h"
@interface ViewController ()
{
Person *person;

}

- (IBAction)test:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
person = [[Person alloc] init];

[person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
//这两种方法都会调用Observer实现的代理方法
//[person setValue:@"lala" forKey:@"name"];

}
-(void)dealloc{
[person removeObserver:self forKeyPath:@"name"];
}

- (IBAction)test:(id)sender {
//取值
NSLog(@"%@",[person valueForKey:@"name"]);
}
/**
*  KVC的代理方法(当属性改变时调用这个方法)
*
*  @param keyPath @"name"
*  @param object  person
*  @param change  改变的
*  @param context 传输的对象
*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
//NSLog(@"%@",[xiaoMing valueForKey:@"name"]);
NSLog(@"change == %@",change);
NSLog(@"object == %@",object);
NSLog(@"context == %@",context);
}
<span style="background-color: rgb(255, 255, 255);">   给属性赋值</span>
<span style="background-color: rgb(255, 255, 255);">    [person setValue:@"lala" forKey:@"name"];
person.name = @"lala";</span>
访问属性值

[person valueForKey:@"name"]


2.0 KVO

Key-Value Observing (KVO) 建立在 KVC 之上,它能够观察一个对象的 KVC key path 值的变化。
创建一个继承于NSObject的Person类

Person.h

#import <Foundation/Foundation.h>

@interface person : NSObject
{
NSString *name;
}
@end


<span style="color:#00afca;">#import "ViewController.h"
#import "person.h"
@interface ViewController (){
person *xiaoMing;
}
- (IBAction)test:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
xiaoMing = [[person alloc] init];
</span><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="font-size:12px;"><span style="color: rgb(147, 200, 106);">[person</span><span style="color: rgb(255, 255, 255);"> </span><span style="color:#00afca;">setValue</span><span style="color: rgb(255, 255, 255);">:</span><span style="color: rgb(228, 68, 72);">@"lala"</span><span style="color: rgb(255, 255, 255);"> </span><span style="color:#00afca;">forKey</span><span style="color: rgb(255, 255, 255);">:</span><span style="color: rgb(228, 68, 72);">@"name"]</span><span style="color:#ffffff;">;</span></span></p><span style="color:#00afca;">
/**
*  添加观察
*/
[xiaoMing addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"test"];
}

- (IBAction)test:(id)sender {
<span style="font-family: Menlo; white-space: pre; color: rgb(147, 200, 106);">[person</span><span style="font-family: Menlo; white-space: pre; color: rgb(255, 255, 255);"> </span><span style="color:#00afca;font-family: Menlo; white-space: pre;">setValue</span><span style="font-family: Menlo; white-space: pre; color: rgb(255, 255, 255);">:</span><span style="font-family: Menlo; white-space: pre; color: rgb(228, 68, 72);">@"haha"</span><span style="font-family: Menlo; white-space: pre; color: rgb(255, 255, 255);"> </span><span style="color:#00afca;font-family: Menlo; white-space: pre;">forKey</span><span style="font-family: Menlo; white-space: pre; color: rgb(255, 255, 255);">:</span><span style="font-family: Menlo; white-space: pre; color: rgb(228, 68, 72);">@"name"]</span><span style="color:#ffffff;font-family: Menlo; white-space: pre;">;</span>

}

/**
*  KVO的代理方法(当属性改变时调用这个方法)
*
*  @param keyPath @"name"
*  @param object  person
*  @param change  改变的
*  @param context 传输的对象
*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"%@",[xiaoMing valueForKey:@"name"]);
NSLog(@"change == %@",change);
NSLog(@"object == %@",object);
NSLog(@"context == %@",context);
}
-(void)dealloc{
[xiaoMing removeObserver:self forKeyPath:@"name"];
}
</span>


当属性值改变是会调用代理方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: