问题描述
有人可以简单地解释什么是Key-Value-Coding
和Key-Value-Observing
吗?请不要提供指向Apple Developer参考文档的链接.我经历了他们.我希望能以非常简单的方式进行解释.
Can someone explain in simple terms what is Key-Value-Coding
and Key-Value-Observing
? Please don't provide links to Apple Developer's reference Document. I have gone through them. I expect an explanation in very simple terms.
推荐答案
键值编码(KVC)意味着使用字符串访问属性或值.
Key-Value-Coding (KVC) means accessing a property or value using a string.
id someValue = [myObject valueForKeyPath:@"foo.bar.baz"];
可能与以下相同:
id someValue = [[[myObject foo] bar] baz];
键值观察(KVO)使您可以观察对属性或值的更改.
Key-Value-Observing (KVO) allows you to observe changes to a property or value.
要使用KVO观察属性,您需要使用字符串将属性标识为;即使用KVC.因此,可观察对象必须符合KVC.
To observe a property using KVO you would identify to property with a string; i.e., using KVC. Therefore, the observable object must be KVC compliant.
[myObject addObserver:self forKeyPath:@"foo.bar.baz" options:0 context:NULL];
这篇关于什么是目标C中的键值编码和键值观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!