项目作者: Kosoku

项目描述 :
Agamotto is an iOS/macOS/tvOS/watchOS framework that provides block based extensions to KVO and NSNotificationCenter.
高级语言: Objective-C
项目地址: git://github.com/Kosoku/Agamotto.git
创建时间: 2017-03-09T07:21:19Z
项目社区:https://github.com/Kosoku/Agamotto

开源协议:MIT License

下载


Agamotto

Carthage compatible
Version
Platform
License

Agamotto is an iOS/macOS/tvOS/watchOS framework that provides block based extensions to KVO and NSNotificationCenter. It handles tearing down the observer upon deallocation. It is based on part of the ReactiveCocoa Objective-C framework.

Installation

You can install Agamotto using cocoapods, Carthage, or as a framework.

Usage

You must do the weakSelf/strongSelf dance for any blocks passed to the observing methods. Otherwise a retain cycle will be introduced.

It is not required in the notification observing example because self is not called within the block.

  1. #import <Agamotto/Agamotto.h>
  2. static NSNotificationName const kTextDidChangeNotification = @"kTextDidChangeNotification";
  3. @interface MyObject : NSObject
  4. @property (copy) NSString *text;
  5. - (void)foo;
  6. @end
  7. @implementation MyObject
  8. - (instancetype)init {
  9. if (!(self = [super init]))
  10. return nil;
  11. __weak __typeof__(self) weakSelf = self;
  12. [self KAG_addObserverForKeyPath:@"text" options:0 block:^(NSString *keyPath, id _Nullable value, NSDictionary<NSKeyValueChangeKey, id> *change){
  13. __strong __typeof__(weakSelf) strongSelf = weakSelf;
  14. [self foo];
  15. }];
  16. [self KAG_addObserverToNotificationCenter:nil notificationName:kTextDidChangeNotification object:self block:^(NSNotification *notification){
  17. NSLog(@"notification %@",notification);
  18. }];
  19. return self;
  20. }
  21. - (void)foo {
  22. NSLog(@"text %@",self.text);
  23. }
  24. - (void)setText:(NSString *)text {
  25. _text = [text copy];
  26. [[NSNotificationCenter defaultCenter] postNotificationName:kTextDidChangeNotification object:self];
  27. }
  28. @end