NSObject+YYAddForKVO.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // NSObject+YYAddForKVO.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/15.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Observer with block (KVO).
  15. */
  16. @interface NSObject (YYAddForKVO)
  17. /**
  18. Registers a block to receive KVO notifications for the specified key-path
  19. relative to the receiver.
  20. @discussion The block and block captured objects are retained. Call
  21. `removeObserverBlocksForKeyPath:` or `removeObserverBlocks` to release.
  22. @param keyPath The key path, relative to the receiver, of the property to
  23. observe. This value must not be nil.
  24. @param block The block to register for KVO notifications.
  25. */
  26. - (void)addObserverBlockForKeyPath:(NSString*)keyPath block:(void (^)(id _Nonnull obj, _Nullable id oldVal, _Nullable id newVal))block;
  27. /**
  28. Stops all blocks (associated by `addObserverBlockForKeyPath:block:`) from
  29. receiving change notifications for the property specified by a given key-path
  30. relative to the receiver, and release these blocks.
  31. @param keyPath A key-path, relative to the receiver, for which blocks is
  32. registered to receive KVO change notifications.
  33. */
  34. - (void)removeObserverBlocksForKeyPath:(NSString*)keyPath;
  35. /**
  36. Stops all blocks (associated by `addObserverBlockForKeyPath:block:`) from
  37. receiving change notifications, and release these blocks.
  38. */
  39. - (void)removeObserverBlocks;
  40. @end
  41. NS_ASSUME_NONNULL_END