一.簡單復(fù)習(xí)一下KVO的使用
- 定義一個(gè)類号枕,繼承自NSObject,并添加一個(gè)name的屬性
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TCPerson : NSObject
@property (nonatomic, copy) NSString *name;
@end
NS_ASSUME_NONNULL_END
- 在ViewController我們簡單的使用一下KVO
#import "ViewController.h"
#import "TCPerson.h"
@interface ViewController ()
@property (nonatomic, strong) TCPerson *person1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person1 = [[TCPerson alloc]init];
self.person1.name = @"liu yi fei";
[self.person1 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}
/// 點(diǎn)擊屏幕出發(fā)改變self.person1的name
/// @param touches touches description
/// @param event event description
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.person1.name = @"cang lao shi";
}
/// 監(jiān)聽回調(diào)
/// @param keyPath 監(jiān)聽的屬性名字
/// @param object 被監(jiān)聽的對象
/// @param change 改變的新/舊值
/// @param context context description
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSLog(@"監(jiān)聽到%@對象的%@發(fā)生了改變%@",object,keyPath,change);
}
/// 移除觀察者
- (void)dealloc{
[self.person1 removeObserver:self forKeyPath:@"name"];
}
@end
當(dāng)點(diǎn)擊屏幕的時(shí)候砸王,控制臺(tái)輸出:
2020-09-24 15:53:52.527734+0800 KVO_TC[9255:98204] 監(jiān)聽到<TCPerson: 0x600003444d10>對象的name發(fā)生了改變{
kind = 1;
new = "cang lao shi";
old = "liu yi fei";
}
二.深入剖析KVO的底層
- 在- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.person1.name = @"cang lao shi";
}我們知道self.person1.name的本質(zhì)是[self.person1 setName:@"cang lao shi"];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// self.person1.name = @"cang lao shi";
[self.person1 setName:@"cang lao shi"];
}
在TCPerson的.m文件推盛,我們從寫setter方法并打斷點(diǎn),可以看到當(dāng)我們點(diǎn)擊屏幕的時(shí)候谦铃,我們發(fā)現(xiàn)進(jìn)入了setter方法:
- (void)setName:(NSString *)name{
_name = name;
}
- 在ViewController我們新建一個(gè)person2,代碼變成了:
#import "ViewController.h"
#import "TCPerson.h"
@interface ViewController ()
@property (nonatomic, strong) TCPerson *person1;
@property (nonatomic, strong) TCPerson *person2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person1 = [[TCPerson alloc]init];
self.person1.name = @"liu yi fei";
[self.person1 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
self.person2 = [[TCPerson alloc] init];
self.person2.name = @"yyyyyyyy";
}
/// 點(diǎn)擊屏幕出發(fā)改變self.person1的name
/// @param touches touches description
/// @param event event description
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.person1.name = @"cang lao shi";
// [self.person1 setName:@"cang lao shi"];
self.person2.name = @"ttttttttt";
}
/// 監(jiān)聽回調(diào)
/// @param keyPath 監(jiān)聽的屬性名字
/// @param object 被監(jiān)聽的對象
/// @param change 改變的新/舊值
/// @param context context description
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSLog(@"監(jiān)聽到%@對象的%@發(fā)生了改變%@",object,keyPath,change);
}
/// 移除觀察者
- (void)dealloc{
[self.person1 removeObserver:self forKeyPath:@"name"];
}
@end
- 注意:當(dāng)我們點(diǎn)擊屏幕的時(shí)候輸出的結(jié)果是:
2020-09-24 16:10:36.750153+0800 KVO_TC[9313:105906] 監(jiān)聽到<TCPerson: 0x600002ce8230>對象的name發(fā)生了改變{
kind = 1;
new = "cang lao shi";
old = "liu yi fei";
}
-
既然我們改變name的值的時(shí)候走的都是setName:setter方法耘成,按理說觀察屬性變化的時(shí)候,person2的值也應(yīng)該被觀察到驹闰,為什么它不會(huì)觀察到person2凿跳?
三.KVO的isa指向
- 上篇文章中我分析了實(shí)例對象,類對象疮方,元類對象的isa控嗜,既然當(dāng)我們改變屬性值的時(shí)候,其本質(zhì)是調(diào)用setter方法骡显,那么在KVO中疆栏,person1和person2的setName方法應(yīng)該存儲(chǔ)在類對象中曾掂,我們先來看看這兩個(gè)實(shí)例對象的isa指向:
打開lldb
(lldb) p self.person1.isa
(Class) $0 = NSKVONotifying_TCPerson
Fix-it applied, fixed expression was:
self.person1->isa
(lldb) p self.person2.isa
(Class) $1 = TCPerson
Fix-it applied, fixed expression was:
self.person2->isa
(lldb)
- 從上面的打印我們看到 self.person1的isa指向了NSKVONotifying_TCPerson,而沒有添加觀察著的self.person2的isa卻指向的是TCPerson
- NSKVONotifying_TCPerson是runtime動(dòng)態(tài)創(chuàng)建的類壁顶,繼承自TCPerson珠洗,其內(nèi)部實(shí)現(xiàn)可以看成(模擬的NSKVONotifying_TCPerson流程,下面代碼不能在xcode中運(yùn)行):
#import "NSKVONotifying_TCPerson.h"
@implementation NSKVONotifying_TCPerson
//NSKVONotifying_TCPerson的set方法實(shí)現(xiàn)若专,其本質(zhì)來自于foundation框架
- (void)setName:(NSString *)name{
_NSSetIntVaueAndNotify();
}
//改變過程
void _NSSetIntVaueAndNotify(){
[self willChangeValueForKey:@"name"];
[super setName:name];
[self didChangeValueForKey:@"name"];
}
//通知觀察者
- (void)didChangeValueForKey:(NSString *key){
[observe observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context];
}
@end
-
未添加觀察self.person2實(shí)例對象的isa指向流程圖:
-
添加觀察self.person1實(shí)例對象的isa指向流程圖:
-
所以KVO其本質(zhì)是動(dòng)態(tài)生成一個(gè)NSKVONotifying_TCPerson類许蓖,繼承自TCPerson,當(dāng)實(shí)例對象添加觀察著之后调衰,實(shí)例對象的isa指向了這個(gè)動(dòng)態(tài)創(chuàng)建的類膊爪,當(dāng)其屬性發(fā)生改變時(shí),調(diào)用的是該類的setter方法嚎莉,而不是父類的類對象中的setter方法