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

时间:2023-11-16 00:06:08

手动设定实例变量的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 = @""; // 改变年龄
} - (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"%@", change);
} @end

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

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