2022-06-25 22:25:06.887730+0800 StudyApp[31993:1358988] age - { kind = 1; new = 20; old = 10;}2022-06-25 22:25:06.887791+0800 StudyApp[31993:1358988] didChangeValueForKey:age,0x600001ce84702022-06-25 22:25:06.887835+0800 StudyApp[31993:1358988] 20实现原理
网上对于这块的解说比力多,我就不重复形貌了,转载两个比力好形貌该原理的流程图。
setValue:forKey:的原理
valueForKey:的原理
答疑
给不存在的属性赋值,会怎么样?
会crash
[self.test setValue(10) forKey"age1"]; // 产生以下crash*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<KVOTest 0x600002ed45f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key age1.'给不存在的属性取值,会怎么样?
会crash
[self.test valueForKey"age1"] // 产生以下Crash*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<KVOTest 0x6000033dc620> valueForUndefinedKey:]: this class is not key value coding-compliant for the key age1.'