美文网首页
KVO原理和基本使用

KVO原理和基本使用

作者: Hunter琼 | 来源:发表于2018-03-07 16:39 被阅读47次

KVO的基本原理:

1 在Apple中的API文档中如下:

Automatic key-value observing is implemented using a technique called isa-swizzling… When an observer is registered for an attribute of an object the isa pointer of the observed object is modified, pointing to an intermediate class rather than at the true class …

2 KVO的基本原理:

1 在前面的文章提到过KVO是基于runtime机制实现的iOS Runtime概念归纳 - 简书

2 当某个类的对象第一次被观察时,系统会在运行期间创建该类的一个派生类,在这个派生类中重写基类中任何被观察属性的setter方法,派生类在被重写的setter方法内实现真正的通知机制,若Class类,那么生成的派生类为NSKVONotifying_Class

3 键值观察通知依赖于NSObject的两个方法:willChagreValueForKey:和didChargeValueForKey,在第一个被观察者属性发送改变之前,willChagreValueForKey方法一直被调用,这就会记录旧的值,而当改变时,调用didCharegeValueForKey调用,继而调用 observeValueForKey:ofObject:change:context:

3 KVO的基本使用

(1) 被观察者类:

KVOModel.h

#define KVOID  @"name"
@interface KVOModel : NSObject
// 观察KVOModel的name属性
@property(nonatomic,copy)NSString *name;
@end

KVOModel.m

import "KVOModel.h"
#import <objc/runtime.h>
@implementation KVOModel
static const char KVOModelKey = '\0';
- (void)setName:(NSString *)name
{
    [self willChangeValueForKey:KVOID];
    objc_setAssociatedObject(self, &KVOModelKey, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self didChangeValueForKey:KVOID];  
}
- (NSString *)name
{
    return objc_getAssociatedObject(self, &KVOModelKey);
    
}

(2)观察者类

#import "ViewController.h"
#import "KVOModel.h"
//NSString *const kv0Offset = @"name";
@interface ViewController ()
@property(nonatomic,strong)KVOModel *model;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.model = [KVOModel  new];
    self.model.name = @"kvo1";
    // 注册观察者
    NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
    [self.model addObserver:self forKeyPath:KVOID options:options context:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self removeObservers];
}

/**
 keypath:属性名称
 object:被观察者对象
 charge:变化前后值都在change中
 context:注册时候,传过来的值
 **/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    
    NSLog(@"keypath=%@, dic==%@",keyPath,change);
    
}
- (IBAction)kvoClicktest:(id)sender {
    
    self.model.name = @"kvo2";
    
}
- (void)removeObservers
{
    // 移除监听
    [self removeObserver:self forKeyPath:KVOID];
}

- (void)dealloc
{
    [self removeObservers];
}

参考文献:
http://www.cocoachina.com/ios/20180305/22453.html

相关文章

  • KVO和KVC的使用及原理解析

    一 KVO基本使用 二 KVO本质原理讲解及代码验证 三 KVC基本使用 四 KVC设值原理 五 KVC取值原理 ...

  • KVO基本使用

    分三部分解释KVO一.KVO基本使用二.KVO原理解析三.自定义实现KVO 一、KVO基本使用 使用KVO,能够非...

  • iOS原理篇(一): KVO实现原理

    KVO实现原理 什么是 KVO KVO 基本使用 KVO 的本质 总结 一 、 什么是KVO KVO(Key-Va...

  • KVO原理和基本使用

    KVO的基本原理: 1 在Apple中的API文档中如下: Automatic key-value observi...

  • iOS KVO

    KVO全称Key-Value Observing,键值监听。 基本使用和原理: KVO的本质: 当我们给对象注册一...

  • 底层探索--KVO、KVC的本质

    KVO 原理 基本使用 //添加监听 [self addObserver:<#(nonnull NSObjec...

  • KVO基本使用和底层原理

    个人搭建的博客,欢迎访问:https://www.pphtc.com/work.html 概述 KVO是Key-V...

  • KVO的原理

    KVO基本原理: KVO深入原理: 适用于:

  • iOS的KVO和KVC底层原理

    1. KVO 一.KVO原理的使用与证明 我们在开发的过程中经常使用KVO和KVC,但是我们并不了解其底层原理和功...

  • 知识集锦

    https://github.com/starainDou 欢迎点星 KVO实现原理 KVO基本原理: 1 kvo...

网友评论

      本文标题:KVO原理和基本使用

      本文链接:https://www.haomeiwen.com/subject/owojfftx.html