• 手动设定实例变量的KVO实现监听


    手动设定实例变量的KVO实现监听

    如果将一个对象设定成属性,这个属性是自动支持KVO的,如果这个对象是一个实例变量,那么,这个KVO是需要我们自己来实现的.

    以下给出源码供君测试:

    Student.h 与 Student.m

    //
    //  Student.h
    //  SuperNotification
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    
    {
        NSString  *_age;
    }
    - (void)setAge:(NSString *)age;
    - (NSString *)age;
    
    @property (nonatomic, strong) NSString  *name;
    
    @end
    //
    //  Student.m
    //  SuperNotification
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "Student.h"
    
    @implementation Student
    
    @synthesize name = _name;
    - (void)setName:(NSString *)name
    {
        _name = name;
    }
    - (NSString *)name
    {
        return _name;
    }
    
    
    // 手动设定KVO
    - (void)setAge:(NSString *)age
    {
        [self willChangeValueForKey:@"age"];
        _age = age;
        [self didChangeValueForKey:@"age"];
    }
    - (NSString *)age
    {
        return _age;
    }
    + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
    {
        // 如果监测到键值为age,则指定为非自动监听对象
        if ([key isEqualToString:@"age"])
        {
            return NO;
        }
        
        return [super automaticallyNotifiesObserversForKey:key];
    }
    
    @end

    具体使用情况:

    //
    //  RootViewController.m
    //  SuperNotification
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "Student.h"
    
    @interface RootViewController ()
    
    @property (nonatomic, strong) Student *student;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 创建学生对象
        _student = [Student new];
        
        // 监听属性name
        [_student addObserver:self
                   forKeyPath:@"name"  // 属性
                      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                      context:nil];
        
        // 监听实例变量age
        [_student addObserver:self
                   forKeyPath:@"age"   // 实例变量
                      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                      context:nil];
        
        _student.name = @"YouXianMing"; // 改变名字
        _student.age  = @"18";          // 改变年龄
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary *)change
                           context:(void *)context
    {
        NSLog(@"%@", change);
    }
    
    @end

    这个是手动实现KVO需要注意的地方:

  • 相关阅读:
    react-native ios打包和Android打包
    用create-react-app创建的react项目怎么解决打包体积过大
    react native 新建项目运行报错
    微信分享等配置,微信授权失败讲解
    微信公众号的code,微信授权
    linux 递归删除指定文件
    mariaDB安装 无密码就可以登录、远程访问、及新版本初始化密码
    mysql 可重复插入、创建语句
    redis数据库入门
    Extjs 3.4 同值合并
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3902827.html
Copyright © 2020-2023  润新知