記 上一篇文章中說到,KVO監聽成員變量沒法收到回調。先驗證一下是否是對的。數組
1.建立一個Person
類,包含一個公有成員變量age
,一個屬性變量name
。bash
@interface Person : NSObject{
@public
int age;
}
@property(strong,nonatomic)NSString * name;
複製代碼
成員變量與屬性的區別: 屬性會自動生成setter
與getter
方法,成員變量不會。ui
2.在ViewController
中分別監聽兩個變量。atom
self.p = [[Person alloc]init];
[self.p addObserver:self forKeyPath:@"name" options:(NSKeyValueObservingOptionNew) context:NULL];
[self.p addObserver:self forKeyPath:@"age" options:(NSKeyValueObservingOptionNew) context:NULL];
複製代碼
3.實現監聽回調,log
出改變內容以及被觀察者。spa
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSLog(@"%@---%@",change,object);
}
複製代碼
4.分別給兩個變量賦值,用於觸發KVO
。code
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.p.name = @"name";
self.p->age = 10;
}
複製代碼
.
點語法與->
的區別: .
點語法調用了setter
方法,而->
是直接訪問成員變量server
5.養成習慣,移除觀察者對象
-(void)dealloc{
[self.p removeObserver:self forKeyPath:@"name"];
[self.p removeObserver:self forKeyPath:@"age"];
}
複製代碼
6.觸發KVO
後的發現log
內容只有一個繼承
2019-03-07 21:53:13.105997+0800 KVO[84542:11106254] {
kind = 1;
new = name;
}---<Person: 0x6000007a6600>
複製代碼
7.在@implementation
中手動爲age
寫setter
方法rem
-(void)setAge:(int)newAge{
age=newAge;
}
複製代碼
8.修改touchesBegan
方法中的age
賦值方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.p.name = @"name";
[self.p setAge:10];
}
複製代碼
9.再次運行程序,觸發KVO
,觀察log
信息,會發現打印了兩次。
2019-03-07 22:24:24.903100+0800 KVO[85477:11167107] {
kind = 1;
new = name;
}---<Person: 0x600002680a40>
2019-03-07 22:24:24.903477+0800 KVO[85477:11167107] {
kind = 1;
new = 10;
}---<Person: 0x600002680a40>
複製代碼
因而可知,只有對象實現了setter
方法,KVO
對其進行的觀察,纔會發起回調。
分別在添加觀察者先後打印一下對象名稱
self.p = [[Person alloc]init];
NSLog(@"%s",object_getClassName(self.p));
[self.p addObserver:self forKeyPath:@"name" options:(NSKeyValueObservingOptionNew) context:NULL];
NSLog(@"------分割線----------");
NSLog(@"%s",object_getClassName(self.p));
複製代碼
打印結果爲:
2019-03-08 10:13:13.099894+0800 KVO[88913:11408301] Person
2019-03-08 10:13:13.101816+0800 KVO[88913:11408301] ------分割線----------
2019-03-08 10:13:13.102021+0800 KVO[88913:11408301] NSKVONotifying_Person
複製代碼
從打印結果咱們能夠觀察到,先前self.p
指向的是Person
,添加觀察者後變成了NSKVONotifying_Person
。由此可知,在咱們爲一個對象添加觀察者以後,KVO
會自動建立一個NSKVONotifying_<ClassName>
。
這裏有一個打印類跟子類的方法
-(void)printClasses:(Class)cls{
//註冊類的總量
int count = objc_getClassList(NULL, 0);
//建立一個數組,其中包含給定對象
NSMutableArray *mArray = [NSMutableArray arrayWithObject:cls];
//獲取全部已經註冊的類
Class *classes = (Class *)malloc(sizeof(Class)*count);
objc_getClassList(classes, count);
for (int i = 0; i<count; i++) {
//classes[i]的父類 等於 cls
if (cls == class_getSuperclass(classes[i])) {
[mArray addObject:classes[i]];
}
}
//釋放classes
free(classes);
NSLog(@"%@",mArray);
}
複製代碼
在添加觀察者前,嘗試打印一下[Person class]
[self printClasses:[Person class]];
複製代碼
打印結果爲:
2019-03-07 22:52:02.545406+0800 KVO[86213:11217129](
Person,
Student
)
複製代碼
而後添加一個觀察者,再從新打印一次
[self.p addObserver:self forKeyPath:@"name" options:(NSKeyValueObservingOptionNew) context:NULL];
[self printClasses:[Person class]];
複製代碼
打印結果爲:
2019-03-07 22:52:02.555585+0800 KVO[86213:11217129] (
Person,
"NSKVONotifying_Person",
Student
)
複製代碼
因此KVO
會自動建立的NSKVONotifying_<ClassName>
類繼承自<ClassName>
。
這裏有一個打印當前類執行的全部方法的方法
-(void)printClassAllMethod:(Class)cls{
unsigned int count = 0;
//獲取當前類對應的方法列表
Method *methods = class_copyMethodList(cls, &count);
for (int i = 0 ; i<count; i++) {
Method method = methods[i];
//分別獲取SEL 跟 IMP
SEL methodSEL = method_getName(method);
IMP methodIMP = class_getMethodImplementation(cls, methodSEL);
//打印SEL名稱以及IMP地址
NSLog(@"%@---%p",NSStringFromSelector(methodSEL),methodIMP);
}
//釋放methods
free(methods);
}
複製代碼
觀察先後若是SEL
數量發生改變,表明有對其進行了新添方法(爲何沒刪除?子類沒辦法操做父類方法)。若是IMP地址發生了改變,表明對這個IMP
進行的重寫。 驗證: 新建一個Student
類繼承Person
Person
中實現方法-(void)say;
Student
重寫-(void)say;
而且實現方法-(void)study;
順便重寫一下原有方法 class
方法 分別打印-(void)printClassAllMethod:(Class)cls
[self printClassAllMethod:[Person class]];
NSLog(@"------分割線----------");
[self printClassAllMethod:[Student class]];
複製代碼
打印結果:
2019-03-08 11:35:52.257273+0800 KVO[90824:11859981] say---0x109dfd260
2019-03-08 11:35:52.257500+0800 KVO[90824:11859981] ------分割線----------
2019-03-08 11:35:52.257659+0800 KVO[90824:11859981] say---0x109dfd1c0
2019-03-08 11:35:52.257774+0800 KVO[90824:11859981] study---0x109dfd1f0
2019-03-08 11:35:52.257976+0800 KVO[90824:11859981] class---0x109dfd220
複製代碼
其中, say
方法重寫,IMP
地址發生改變,而且打印出了新增的study
方法以及重寫的class
方法。
分別打印[Person class]
與[NSKVONotifying_Person class]
,對比原類跟新類都執行了什麼方法。
[self printClassAllMethod:[Person class]];
[self.p addObserver:self forKeyPath:@"name" options:(NSKeyValueObservingOptionNew) context:NULL];
NSLog(@"------分割線----------");
[self printClassAllMethod:NSClassFromString(@"NSKVONotifying_Person")];
複製代碼
打印結果:
2019-03-08 10:47:01.419689+0800 KVO[89658:11656677] .cxx_destruct---0x10b19c1b0
2019-03-08 10:47:01.419936+0800 KVO[89658:11656677] name---0x10b19c150
2019-03-08 10:47:01.420134+0800 KVO[89658:11656677] setName:---0x10b19c170
2019-03-08 10:47:01.421109+0800 KVO[89658:11656677] ------分割線----------
2019-03-08 10:47:01.421352+0800 KVO[89658:11656677] setName:---0x10b4f663a
2019-03-08 10:47:01.421516+0800 KVO[89658:11656677] class---0x10b4f506e
2019-03-08 10:47:01.421649+0800 KVO[89658:11656677] dealloc---0x10b4f4e12
2019-03-08 10:47:01.421782+0800 KVO[89658:11656677] _isKVOA---0x10b4f4e0a
複製代碼
因此,NSKVONotifying_Person
重寫了setter
、class
、dealloc
添加了_isKVOA
。
setter
方法,KVO
對其進行的觀察,纔會發起回調。KVO
會自動建立一個NSKVONotifying_<ClassName>
。NSKVONotifying_<ClassName>
類繼承自<ClassName>
。NSKVONotifying_<ClassName>
重寫了setter
、class
、dealloc
,添加了_isKVOA
。