校园网站的建设费用,长沙app制作公司哪家好,豆瓣中需要优化的网站标签,虚拟主机建立网站1、KVC#xff0c;即是指 NSKeyValueCoding#xff0c;一个非正式的Protocol#xff0c;提供一种机制来间接访问对象的属性。而不是通过调用Setter、Getter方法访问。KVO 就是基于 KVC 实现的关键技术之一。 Demo#xff1a; interface myPerson : NSObject { …1、KVC即是指 NSKeyValueCoding一个非正式的Protocol提供一种机制来间接访问对象的属性。而不是通过调用Setter、Getter方法访问。KVO 就是基于 KVC 实现的关键技术之一。 Demo interface myPerson : NSObject { NSString*_name; int _age; int _height; int _weight; } end interface testViewController :UIViewController property (nonatomic, retain) myPerson*testPerson; end - (void)testKVC { testPerson [[myPerson alloc] init]; NSLog(testPerson‘s init height %, [testPerson valueForKey:height]); [testPerson setValue:[NSNumber numberWithInt:168]forKey:height]; NSLog(testPerson‘s height %, [testPerson valueForKey:height]); } 第一段代码是定义了一个myPerson的类这个类有一个_height的属性但是没有提供任何gettersetter的访问方法。同时在testViewController这个类里面有一个myPerson的对象指针。 当myPerson实例化后常规来说是无法访问这个对象的_height属性的不过通过KVC我们做到了代码就是testKVC这个函数。 kvc除了访问私有变量这个用处外,还可以用于字典转模型。在Person类对外提供一个接口,将转模型的工作放在模型中进行 - (instancetype)initWithDict:(NSDictionary *)dict{ if (self [super init]) { [self setValuesForKeysWithDictionary:dict]; } returnself; } 外面可以直接将字典传入,和平常转模型相比,kvc更加方便,减少了代码量。 NSDictionary*PersonDict {name:李四,age:18}; Person *p2 [Person personWithDict:PersonDict]; NSLog(name %,age %ld,p2.name,p2.age); 所以kvc最常见的两种用法就是: 1对私有变量进行赋值 2字典转模型 但是也有一些需要注意的地方1字典转模型的时候,字典中的某一个key一定要在模型中有对应的属性 2如果一个模型中包含了另外的模型对象,是不能直接转化成功的。 3通过kvc转化模型中的模型,也是不能直接转化成功的。 既然可以通过kvc赋值,同样的也可以通过它进行取值。 NSLog(name%,[p2 valueForKey:name]);
NSLog(dogweight%, [p2.dog valueForKeyPath:p2.dog]); kvc的查询机制 程序优先调用name代码来获取该 getter方法 的返回值如果没有getter方法那么寻找该类名为 _name的成员变量。如果没有_name成员变量就继续寻找那么就寻找该类名为 name的成员变量。如果上面的三条都没有达到那么系统会自动调用该对象的 valueforUndefinedKey:方法。 二KVO KVO的是KeyValue Observe的缩写中文是键值观察。这是一个典型的观察者模式观察者在键值改变时会得到通知。iOS中有个Notification的机制也可以获得通知但这个机制需要有个Center相比之下KVO更加简洁而直接。 KVO的使用也很简单就是简单的3步。 1.注册需要观察的对象的属性addObserver:forKeyPath:options:context: 2.实现observeValueForKeyPath:ofObject:change:context:方法这个方法当观察的属性变化时会自动调用 3.取消注册观察removeObserver:forKeyPath:context: Demo interface myPerson : NSObject { NSString *_name; int _age; int _height; int _weight; } end interface testViewController : UIViewController property (nonatomic, retain) myPerson *testPerson; - (IBAction)onBtnTest:(id)sender; end - (void)testKVO { testPerson [[myPerson alloc] init]; [testPerson addObserver:self forKeyPath:height options:NSKeyValueObservingOptionNew context:nil]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:height]) { NSLog(Height is changed! new%, [change valueForKey:NSKeyValueChangeNewKey]); } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } - (IBAction)onBtnTest:(id)sender { int h [[testPerson valueForKey:height] intValue]; [testPerson setValue:[NSNumber numberWithInt:h1] forKey:height]; NSLog(person height%, [testPerson valueForKey:height]); } - (void)dealloc { [testPerson removeObserver:self forKeyPath:height context:nil]; [super dealloc]; } 第一段代码声明了myPerson类里面有个_height的属性。在testViewController有一个testPerson的对象指针。 在testKVO这个方法里面我们注册了testPerson这个对象height属性的观察这样当testPerson的height属性变化时 会得到通知。在这个方法中还通过NSKeyValueObservingOptionNew这个参数要求把新值在dictionary中传递过来。 重写了observeValueForKeyPath:ofObject:change:context:方法这个方法里的change这个NSDictionary对象包含了相应的值。 需要强调的是KVO的回调要被调用属性必须是通过KVC的方法来修改的如果是调用类的其他方法来修改属性这个观察者是不会得到通知的。 KVO和NSNotification的区别 和delegate一样KVO和NSNotification的作用也是类与类之间的通信与delegate不同的是1这两个都是负责发出通知剩下的事情就不管了所以没有返回值2delegate只是一对一而这两个可以一对多。这两者也有各自的特点。