KeyValueObserver.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // KeyValueObserver.h
  3. // Lab Color Space Explorer
  4. //
  5. // Created by Daniel Eggert on 01/12/2013.
  6. // Copyright (c) 2013 objc.io. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface KeyValueObserver : NSObject
  10. @property (nonatomic, weak) id target;
  11. @property (nonatomic) SEL selector;
  12. /// Create a Key-Value Observing helper object.
  13. ///
  14. /// As long as the returned token object is retained, the KVO notifications of the @c object
  15. /// and @c keyPath will cause the given @c selector to be called on @c target.
  16. /// @a object and @a target are weak references.
  17. /// Once the token object gets dealloc'ed, the observer gets removed.
  18. ///
  19. /// The @c selector should conform to
  20. /// @code
  21. /// - (void)nameDidChange:(NSDictionary *)change;
  22. /// @endcode
  23. /// The passed in dictionary is the KVO change dictionary (c.f. @c NSKeyValueChangeKindKey, @c NSKeyValueChangeNewKey etc.)
  24. ///
  25. /// @returns the opaque token object to be stored in a property
  26. ///
  27. /// Example:
  28. ///
  29. /// @code
  30. /// self.nameObserveToken = [KeyValueObserver observeObject:user
  31. /// keyPath:@"name"
  32. /// target:self
  33. /// selector:@selector(nameDidChange:)];
  34. /// @endcode
  35. + (NSObject *)observeObject:(id)object keyPath:(NSString*)keyPath target:(id)target selector:(SEL)selector __attribute__((warn_unused_result));
  36. /// Create a key-value-observer with the given KVO options
  37. + (NSObject *)observeObject:(id)object keyPath:(NSString*)keyPath target:(id)target selector:(SEL)selector options:(NSKeyValueObservingOptions)options __attribute__((warn_unused_result));
  38. /**
  39. * When you call this method, observer will not work.
  40. * Please call observer method again.
  41. */
  42. - (void)unObserver;
  43. @end