Skip to content
Browse files

Update category with code from other branches

  • Loading branch information...
1 parent b5b189a commit 14a1c721c82a173e6197cd039b3aae3ffbfd8816 @Abizern committed Jan 27, 2014
Showing with 22 additions and 19 deletions.
  1. +1 −5 NSObject+JCSKVOWithBlocks.h
  2. +21 −14 NSObject+JCSKVOWithBlocks.m
View
6 NSObject+JCSKVOWithBlocks.h
@@ -32,14 +32,10 @@
*/
-#if !__has_feature(objc_arc)
-#warning "NSObject+JCSKVOWithBlocks only runs under ARC."
-#endif
+#import <Foundation/Foundation.h>
typedef void (^jcsObservationBlock)(NSDictionary *change);
-#import <Foundation/Foundation.h>
-
@interface NSObject (JCSKVOWithBlocks)
/** Register to observe a keypath
View
35 NSObject+JCSKVOWithBlocks.m
@@ -22,21 +22,25 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
+#if !__has_feature(objc_arc)
+#warning "NSObject+JCSKVOWithBlocks only runs under ARC."
+#endif
+
#ifndef DEBUG
#ifndef NS_BLOCK_ASSERTIONS
#define NS_BLOCK_ASSERTIONS
#endif
#endif
+#import "NSObject+JCSKVOWithBlocks.h"
+#import <objc/runtime.h>
+
// The context for a KVO observations
-static NSString * const JCSKVOWithBlocksObservationContext = @"JCSKVOWithBlocksObservationContext";
+static void* kJCSKVOWithBlocksObservationContext = &kJCSKVOWithBlocksObservationContext;
// The key under which the array of observers is stored in associated objects
-static NSString * const JCSKVOWithBlocksObserverAssociatedObjectKey = @"com.junglecandy.jcskvowithblocks";
-
+static void* kJCSKVOWithBlocksObserverAssociatedObjectKey = &kJCSKVOWithBlocksObserverAssociatedObjectKey;
-#import "NSObject+JCSKVOWithBlocks.h"
-#import <objc/runtime.h>
#pragma mark JCSKVOObserver
@@ -61,18 +65,21 @@ - (id)initWithKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions) o
NSParameterAssert(block);
self = [super init];
- _observedKeyPath = [keyPath copy];
- _options = options;
- _queue = queue;
- _block = [block copy];
+ if( self )
+ {
+ _observedKeyPath = [keyPath copy];
+ _options = options;
+ _queue = queue;
+ _block = [block copy];
+ }
return self;
}
#pragma mark - NSKeyValueObserving
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- if (!(context == (__bridge void *)(JCSKVOWithBlocksObservationContext))) {
+ if (context != kJCSKVOWithBlocksObservationContext) {
return [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
@@ -96,14 +103,14 @@ @implementation NSObject (JCSKVOWithBlocks)
- (id)jcsAddObserverForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options queue:(NSOperationQueue *)queue block:(jcsObservationBlock)block {
id observer = [[JCSKVOObserver alloc] initWithKeyPath:keyPath options:options queue:queue block:block];
- [self addObserver:observer forKeyPath:keyPath options:options context:(__bridge void *)(JCSKVOWithBlocksObservationContext)];
+ [self addObserver:observer forKeyPath:keyPath options:options context: kJCSKVOWithBlocksObservationContext];
dispatch_sync([self jcsKVOWithBlocksQueue], ^{
- NSMutableArray *observers = objc_getAssociatedObject(self, (__bridge const void *)(JCSKVOWithBlocksObserverAssociatedObjectKey));
+ NSMutableArray *observers = objc_getAssociatedObject(self, kJCSKVOWithBlocksObserverAssociatedObjectKey);
if (!observers) {
observers = [NSMutableArray new];
- objc_setAssociatedObject(self, (__bridge const void *)(JCSKVOWithBlocksObserverAssociatedObjectKey), observers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(self, kJCSKVOWithBlocksObserverAssociatedObjectKey, observers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
[observers addObject:observer];
@@ -126,7 +133,7 @@ - (void)jcsRemoveObserver:(id)observer {
NSAssert([observer isMemberOfClass:[JCSKVOObserver class]], @"The observer has to be an instance of JCSKVOObserver");
dispatch_sync([self jcsKVOWithBlocksQueue], ^{
- NSMutableArray *observers = objc_getAssociatedObject(self, (__bridge const void *)(JCSKVOWithBlocksObserverAssociatedObjectKey));
+ NSMutableArray *observers = objc_getAssociatedObject(self, kJCSKVOWithBlocksObserverAssociatedObjectKey);
if (!observers || ![observers containsObject:observer]) {
return;

0 comments on commit 14a1c72

Please sign in to comment.
Something went wrong with that request. Please try again.