Skip to content

Commit

Permalink
fix KVOBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakey committed Mar 27, 2018
1 parent ed8de9d commit f84d28e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
48 changes: 47 additions & 1 deletion JKCategories/Foundation/NSObject/NSObject+JKKVOBlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,71 @@

// This class is based on NSObject+KVOBlocks (MIT licensed) by Stephan Leroux.
// See here: https://github.com/sleroux/KVO-Blocks


/*
UIViewController *observer = self;
if (self.observersOn) {
// This is where the magic happens
[self.user jk_addObserver:observer forKeyPath:@"email" options:0 context:nil withBlock:^(NSDictionary *change, void *context) {
NSLog(@"Changed email");
}];
[self.user jk_addObserver:observer forKeyPath:@"username" options:0 context:nil withBlock:^(NSDictionary *change, void *context) {
NSLog(@"Changed username");
}];
} else {
[self.user jk_removeBlockObserver:observer forKeyPath:@"username"];
[self.user jk_removeBlockObserver:observer forKeyPath:@"email"];
}
*/


#import <Foundation/Foundation.h>
typedef void (^JKKVOBlock)(NSDictionary *change, void *context);

@interface NSObject (JKKVOBlocks)

/**
添加观察者与监听属性
@param observer 观察者,一般为其他对象(谁想监听)
@param keyPath 监听的属性
@param options 监听模式
@param context context
@param block 监听回调
*/
- (void)jk_addObserver:(NSObject *)observer
forKeyPath:(NSString *)keyPath
options:(NSKeyValueObservingOptions)options
context:(void *)context
withBlock:(JKKVOBlock)block;
/**
移除观察者对属性的监听
@param observer 观察者,一般为其他对象(谁想监听)
@param keyPath 监听的属性
*/
-(void)jk_removeBlockObserver:(NSObject *)observer
forKeyPath:(NSString *)keyPath;

/**
对象本身作为观察者
@param keyPath 监听的属性
@param options 监听模式
@param context context
@param block 监听回调
*/
-(void)jk_addObserverForKeyPath:(NSString *)keyPath
options:(NSKeyValueObservingOptions)options
context:(void *)context
withBlock:(JKKVOBlock)block;

/**
移除观察者对属性的监听
@param keyPath 监听的属性
*/
-(void)jk_removeBlockObserverForKeyPath:(NSString *)keyPath;

@end
8 changes: 5 additions & 3 deletions JKCategories/Foundation/NSObject/NSObject+JKKVOBlocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ -(void)jk_removeBlockObserver:(NSObject *)observer
[self removeObserver:observer forKeyPath:keyPath];
}

-(void)jk_observeValueForKeyPath:(NSString *)keyPath
-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {

JKKVOBlock block = objc_getAssociatedObject(self, (__bridge const void *)(keyPath));
block(change, context);
if (block) {
block(change, context);
}
}

-(void)jk_addObserverForKeyPath:(NSString *)keyPath
Expand All @@ -67,4 +69,4 @@ -(void)jk_removeBlockObserverForKeyPath:(NSString *)keyPath {
[self jk_removeBlockObserver:self forKeyPath:keyPath];
}

@end
@end

0 comments on commit f84d28e

Please sign in to comment.