做网站代码第一不,手机软件app开发,浙江建设集团网站,推广搜索怎么选关键词对KVC和KVO的理解 对KVC和KVO的理解 kvc kvo KVC KVC是KeyValueCoding的简称#xff0c;它是一种可以直接通过字符串的名字(key)来访问类属性的机制。而不是通过调用Setter、Getter方法访问。 KVC实例 一个对象拥有某些属性。比如说#xff0c;一个Person对象有一个name和一个… 对KVC和KVO的理解 对KVC和KVO的理解 kvc kvo KVC KVC是KeyValueCoding的简称它是一种可以直接通过字符串的名字(key)来访问类属性的机制。而不是通过调用Setter、Getter方法访问。 KVC实例 一个对象拥有某些属性。比如说一个Person对象有一个name和一个address属性。以KVC为例Person对象分别有一个value对应他的name和address的key。key只是一个字符串它对应的值可以是任意类型的对象。从最基础的层次上看KVC有两个方法一个是设置 key的值另一个是获取 key 的值。如下面的例子 *void changeName(Person *p, NSString *newName){ // using the KVC accessor (getter) method
NSString *originalName [p valueForKey:name];
// using the KVC accessor (setter) method.
[p setValue:newName forKey:name];
NSLog(Changed %s name to: %, originalName, newName);}* 现在如果Person有另外一个key配偶spousespouse的key 值是另一个Person对象用KVC可以这样写 *void logMarriage(Person *p){ // just using the accessor again, same as example above NSString *personsName [p valueForKey:name]; // this line is different, because it is using // a key path instead of a normal key NSString spousesName [p valueForKeyPath:spouse.name]; NSLog(% is happily married to %, personsName, spousesName); } key与keypath要区分开来key 可以从一个对象中获取值而 keyPath可以将多个key用点号 “.” 分割连接起来比如 [p valueForKeyPath:spouse.name]; 相当于这样… [[p valueForKey:spouse] valueForKey:name]; KVO KVO是KeyValueObserving的简称它提供一种机制当指定的对象的属性被修改后则对象就会接受到通知。简单的说就是每次指定的被观察的对象的属性被修改后KVO就会自动通知相应的观察者了。 KVO实例 KeyValueObserving(KVO)建立在 KVC之上它能够观察一个对象的KVC key path 值的变化。举个例子用代码观察一个person对象的address 变化以下是实现的三个方法 - watchPersonForChangeOfAddress: 实现观察 - observeValueForKeyPath:ofObject:change:context: 在被观察的 key path 的值变化时调用。 - dealloc 停止观察 static NSString *const KVO_CONTEXT_ADDRESS_CHANGED KVO_CONTEXT_ADDRESS_CHANGEDimplementation PersonWatcher -(void) watchPersonForChangeOfAddress:(Person *)p { // this begins the observing [p addObserver:self forKeyPath:address options:0 context:KVO_CONTEXT_ADDRESS_CHANGED]; // keep a record of all the people being observed, // because we need to stop observing them in dealloc [m_observedPeople addObject:p]; } // whenever an observed key path changes, this method will be called - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { // use the context to make sure this is a change in the address, // because we may also be observing other things if(context KVO_CONTEXT_ADDRESS_CHANGED) { NSString *name [object valueForKey:name]; NSString *address [object valueForKey:address]; NSLog(% has a new address: %, name, address); } } -(void) dealloc; { // must stop observing everything before this object is // deallocated, otherwise it will cause crashes for(Person *p in m_observedPeople){ [p removeObserver:self forKeyPath:address]; } [m_observedPeople release]; m_observedPeople nil; [super dealloc]; } -(id) init; { if(self [super init]){ m_observedPeople [NSMutableArray new]; } return self; } end 这就是 KVO 的作用它通过 key path 观察对象的值当值发生变化的时候会收到通知。 转载于:https://www.cnblogs.com/AliliWl/p/4168105.html