Key-Value-Observing OC&Swift
KVC 支持实例变量,KVO 只能手动支持手动设定实例变量的KVO实现监听。
KVO通过set方法来通知。或者KVC能找到那个实例变量。
属性一:observationInfo
1,只要是继承与NSObject的对象都有observationInfo属性.
2,observationInfo是系统通过分类给NSObject增加的属性。
3,分类文件是NSKeyValueObserving.h这个文件
4,这个属性中存储有属性的监听者,通知者,还有监听的keyPath,等等KVO相关的属性。
5,observationInfo是一个void *指针,指向一个包含所有观察者的一个标识信息对象,信息包含了每个监听的观察者,注册时设定的选项等
OC
[theModel addObserver:self forKeyPath:@"array" options:@[NSKeyValueChangeNewKey, NSKeyValueChangeOldKey] context:(__bridge void * _Nullable)(self)];
NSLog(@"%@", theModel.observationInfo);
方法一:observeValueForKeyPath:ofObject:change:context:
Discussion:监听前先addObserver:forKeyPath:options:context:。监听对象释放前要removeObserver:forKeyPath:或者removeObserver:forKeyPath:context:
OC
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey, id> *)change context:(void *)context;
方法二:addObserver:forKeyPath:options:context:
OC
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
[theModel addObserver:self forKeyPath:@"array" options:@[NSKeyValueChangeNewKey, NSKeyValueChangeOldKey] context:(__bridge void * _Nullable)(self)];
方法三:removeObserver:forKeyPath:
OC
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
方法四:removeObserver:forKeyPath:context:
Discussion:在上下文中检查值,您可以精确地确定哪个addObserver:forKeyPath:options:context:调用用于创建观察关系。当同一个观察者多次为同一个键路径注册,但是使用不同的上下文指针时,应用程序可以明确地确定停止观察哪个对象。调用removeObserver:forKeyPath:context:
OC
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context;
方法五:willChangeValueForKey:
Discussion:两个方法一组,主要是用来是手动发送通知。
OC
- (void)willChangeValueForKey:(NSString *)key;
- (void)setKey:(NSString *)key {
[self willChangeValueForKey:@"key"];
_key = key;
[self didChangeValueForKey:@"key"];
}
方法六:didChangeValueForKey:
OC
- (void)didChangeValueForKey:(NSString *)key;
- (void)setKey:(NSString *)key {
[self willChangeValueForKey:@"key"];
_key = key;
[self didChangeValueForKey:@"key"];
}
方法七:willChange:valuesAtIndexes:forKey:
Discussion:使用于有序的集合类型。比如NSArray,NSOrderedSet。NSKeyValueChange是操作类型,NSIndexSet是操作的位置
OC
- (void)willChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key;
方法八:didChange:valuesAtIndexes:forKey:
OC
- (void)didChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key;
方法九:willChangeValueForKey:withSetMutation:usingObjects:
Discussion:用于无序的集合,比如NSSet。NSKeyValueSetMutationKind是操作类型,NSSet是用于对比的set。比如NSKeyValueUnionSetMutation就是把set中的元素加到key集合里。
OC
- (void)willChangeValueForKey:(NSString *)key withSetMutation:(NSKeyValueSetMutationKind)mutationKind usingObjects:(NSSet *)objects;
方法十:didChangeValueForKey:withSetMutation:usingObjects:
OC
- (void)didChangeValueForKey:(NSString *)key withSetMutation:(NSKeyValueSetMutationKind)mutationKind usingObjects:(NSSet *)objects;
方法十一:automaticallyNotifiesObserversForKey:
Discussion:每个perproty会有一个+ (BOOL)automaticallyNotifiesObserversOfKey方法。如果重写了这个方法没有重写automaticallyNotifiesObserversForKey的话以automaticallyNotifiesObserversOf<Key>的返回值为准。如果automaticallyNotifiesObserversForKey和automaticallyNotifiesObserversOf<Key>都实现了。以automaticallyNotifiesObserversForKey返回值为准。
OC
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key;
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
return NO;
}
方法十二:keyPathsForValuesAffectingValueForKey:
Discussion:绑定其他keyPaths。比如有3个属性。name firstName lastName。name=firstName+lastName。当firstName lastName改变时name也改变了。也有对应的+ (NSSet )keyPathsForValuesAffectingAccountFor<Key>
+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key;
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"fullName"])
{
NSSet *affectingKeys = [NSSet setWithObjects:@"lastName", @"firstName",nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKeys];
}
return keyPaths;
}
枚举一: NSKeyValueChange
OC
typedef enum NSKeyValueChange : NSUInteger {
NSKeyValueChangeSetting = 1,
//set方法,比如[self setValue: forKey:];
NSKeyValueChangeInsertion = 2,
//集合插入,比如[NSMutableArray addobject:]
NSKeyValueChangeRemoval = 3,
//集合删除,比如[NSMutableArray removeall]
NSKeyValueChangeReplacement = 4
//集合替代,
} NSKeyValueChange;
枚举二:NSKeyValueObservingOptions
OC
typedef enum NSKeyValueObservingOptions : NSUInteger {
NSKeyValueObservingOptionNew = 0x01,
//更改字典提供新属性值
NSKeyValueObservingOptionOld = 0x02,
//更改字典提供旧属性值
NSKeyValueObservingOptionInitial = 0x04,
//init时通知
NSKeyValueObservingOptionPrior = 0x08
//是否应该在每个更改之前和之后分别向观察者发送通知,而不是在更改之后发送一个通知。在值改变之前发送的change notification中,change参数会包含NSKeyValueChangeNotificationIsPriorKey并且值为@YES,但不会包含NSKeyValueChangeNewKey和它对应的值。值改变后不会有NSKeyValueChangeNotificationIsPriorKey。
} NSKeyValueObservingOptions;
枚举三:NSKeyValueChangeKey
OC
FOUNDATION_EXPORT NSKeyValueChangeKey const NSKeyValueChangeKindKey;
//操作类型。参考NSKeyValueChangeKind
FOUNDATION_EXPORT NSKeyValueChangeKey const NSKeyValueChangeNewKey;
//新的值,对应NSKeyValueObservingOptionNew
FOUNDATION_EXPORT NSKeyValueChangeKey const NSKeyValueChangeOldKey;
//旧的值,对应NSKeyValueObservingOptionOld
FOUNDATION_EXPORT NSKeyValueChangeKey const NSKeyValueChangeIndexesKey;
//对有序的集合做NSKeyValueChangeInsertion, NSKeyValueChangeRemoval,NSKeyValueChangeReplacement时返回一个NSIndexSet
FOUNDATION_EXPORT NSKeyValueChangeKey const NSKeyValueChangeNotificationIsPriorKey API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
//对应NSKeyValueObservingOptionPrior
枚举四:NSKeyValueSetMutationKind
OC
typedef enum NSKeyValueSetMutationKind : NSUInteger {
NSKeyValueUnionSetMutation = 1,
//正在将指定集合中的观察者添加到被观察对象中。
//[setKVO unionSet:[[NSSet alloc] initWithArray:@[@10, @11, @12]]];
//指定集合中的观察者正在从被观察对象中移除。
//[setKVO minusSet:[[NSSet alloc] initWithArray:@[@3]]];
NSKeyValueIntersectSetMutation = 3,
//不在指定集合中的观察者正在从被观察对象中移除。
//[setKVO intersectSet:[[NSSet alloc] initWithArray:@[@0, @1, @2]]];
NSKeyValueSetSetMutation = 4
//一组观察者正在替换被观察对象中的现有对象。
//[setKVO setSet:[[NSSet alloc] initWithArray:@[@0, @1, @2]]];
} NSKeyValueSetMutationKind;