1. 程式人生 > >KVO-基本使用方法-底層原理探究-自定義KVO-對容器類的監聽

KVO-基本使用方法-底層原理探究-自定義KVO-對容器類的監聽

書讀百變,其義自見!

將KVO形式以程式碼實現呈現,通俗易懂,更容易掌握 :GitHub

一、KVO-基本使用方法

 

//註冊
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;

//監聽方法
- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id
)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context; //移除 - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(nullable void *)context API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)); - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
//監聽模式(手動,自動),預設是自動Yes +(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key; //屬性的依賴,返回監聽屬性類的集合 +(NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key;