KVC/KVO简单用法

5 篇文章 0 订阅
2 篇文章 0 订阅

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>

当属性值改变是会调用代理方法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值