NSObject+YYAddForKVO.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // NSObject+YYAddForKVO.m
  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 "NSObject+YYAddForKVO.h"
  12. #import "YYKitMacro.h"
  13. #import <objc/objc.h>
  14. #import <objc/runtime.h>
  15. YYSYNTH_DUMMY_CLASS(NSObject_YYAddForKVO)
  16. static const int block_key;
  17. @interface _YYNSObjectKVOBlockTarget : NSObject
  18. @property (nonatomic, copy) void (^block)(__weak id obj, id oldVal, id newVal);
  19. - (id)initWithBlock:(void (^)(__weak id obj, id oldVal, id newVal))block;
  20. @end
  21. @implementation _YYNSObjectKVOBlockTarget
  22. - (id)initWithBlock:(void (^)(__weak id obj, id oldVal, id newVal))block {
  23. self = [super init];
  24. if (self) {
  25. self.block = block;
  26. }
  27. return self;
  28. }
  29. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  30. if (!self.block) return;
  31. BOOL isPrior = [[change objectForKey:NSKeyValueChangeNotificationIsPriorKey] boolValue];
  32. if (isPrior) return;
  33. NSKeyValueChange changeKind = [[change objectForKey:NSKeyValueChangeKindKey] integerValue];
  34. if (changeKind != NSKeyValueChangeSetting) return;
  35. id oldVal = [change objectForKey:NSKeyValueChangeOldKey];
  36. if (oldVal == [NSNull null]) oldVal = nil;
  37. id newVal = [change objectForKey:NSKeyValueChangeNewKey];
  38. if (newVal == [NSNull null]) newVal = nil;
  39. self.block(object, oldVal, newVal);
  40. }
  41. @end
  42. @implementation NSObject (YYAddForKVO)
  43. - (void)addObserverBlockForKeyPath:(NSString *)keyPath block:(void (^)(__weak id obj, id oldVal, id newVal))block {
  44. if (!keyPath || !block) return;
  45. _YYNSObjectKVOBlockTarget *target = [[_YYNSObjectKVOBlockTarget alloc] initWithBlock:block];
  46. NSMutableDictionary *dic = [self _yy_allNSObjectObserverBlocks];
  47. NSMutableArray *arr = dic[keyPath];
  48. if (!arr) {
  49. arr = [NSMutableArray new];
  50. dic[keyPath] = arr;
  51. }
  52. [arr addObject:target];
  53. [self addObserver:target forKeyPath:keyPath options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
  54. }
  55. - (void)removeObserverBlocksForKeyPath:(NSString *)keyPath {
  56. if (!keyPath) return;
  57. NSMutableDictionary *dic = [self _yy_allNSObjectObserverBlocks];
  58. NSMutableArray *arr = dic[keyPath];
  59. [arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
  60. [self removeObserver:obj forKeyPath:keyPath];
  61. }];
  62. [dic removeObjectForKey:keyPath];
  63. }
  64. - (void)removeObserverBlocks {
  65. NSMutableDictionary *dic = [self _yy_allNSObjectObserverBlocks];
  66. [dic enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSArray *arr, BOOL *stop) {
  67. [arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
  68. [self removeObserver:obj forKeyPath:key];
  69. }];
  70. }];
  71. [dic removeAllObjects];
  72. }
  73. - (NSMutableDictionary *)_yy_allNSObjectObserverBlocks {
  74. NSMutableDictionary *targets = objc_getAssociatedObject(self, &block_key);
  75. if (!targets) {
  76. targets = [NSMutableDictionary new];
  77. objc_setAssociatedObject(self, &block_key, targets, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  78. }
  79. return targets;
  80. }
  81. @end