Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
Added correct removeObserver API that had been missing in a while (I …
Browse files Browse the repository at this point in the history
…am a bad bad bad dev)

Fixed unit tests.
Removed useless UI target.
Added command line tester tool.
Some other code reorgs.

--HG--
rename : CKVOBlockNotificationCenter.h => NSObject_KVOBlockNotificationExtensions.h
rename : CKVOBlockNotificationCenter.m => NSObject_KVOBlockNotificationExtensions.m
rename : Test/Unit Tests-Info.plist => Test/Unit Tests/Unit Tests-Info.plist
rename : Unit Tests/UnitTests.h => Test/Unit Tests/UnitTests.h
  • Loading branch information
schwa committed Jul 1, 2010
1 parent 999ae42 commit f81c222
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 4,418 deletions.
4 changes: 2 additions & 2 deletions CKVOBlockNotificationHelper.h
Expand Up @@ -29,9 +29,9 @@

#import <Foundation/Foundation.h>

#import "CKVOBlockNotificationCenter.h"
#import "NSObject_KVOBlockNotificationExtensions.h"

/// You should not have to use CKVOBlockNotificationHelper. It is an internal helper class used by CKVOBlockNotificationCenter.
/// You should not have to use CKVOBlockNotificationHelper. It is an internal helper class used by the NSObject_KVOBlockNotificationExtensions category. Use that instead.
@interface CKVOBlockNotificationHelper : NSObject {
__weak id target;
NSString *keyPath;
Expand Down
@@ -1,5 +1,5 @@
//
// CKVOBlockNotificationCenter.h
// NSObject_KVOBlockNotificationExtensions.h
// MOO
//
// Created by Jonathan Wight on 6/20/09.
Expand Down Expand Up @@ -32,9 +32,9 @@
typedef void (^KVOBlock)(NSString *keyPath, id object, NSDictionary *change, id identifier);

/// KVOBlock extensions to NSObject allow any object to easily register (add) and unregister (remove) block based notifications.
@interface NSObject (NSObject_KVOBlockNotificationCenterExtensions)
@interface NSObject (NSObject_KVOBlockNotificationExtensions)

- (void)addObserver:(NSObject *)observer handler:(KVOBlock)inHandler forKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions identifier:(id)inIdentifier;
- (void)removeObserver:(NSObject *)observer handler:(KVOBlock)inHandler forKeyPath:(NSString *)keyPath;
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)inKeyPath identifier:(id)inIdentifier;

@end
@@ -1,5 +1,5 @@
//
// CKVOBlockNotificationCenter.m
// NSObject_KVOBlockNotificationExtensions.m
// MOO
//
// Created by Jonathan Wight on 6/20/09.
Expand Down Expand Up @@ -28,7 +28,7 @@
//


#import "CKVOBlockNotificationCenter.h"
#import "NSObject_KVOBlockNotificationExtensions.h"

#import "CKVOBlockNotificationHelper.h"
#include <objc/runtime.h>
Expand All @@ -43,17 +43,17 @@ static id KeyForTarget(id inObserver, id inTarget, NSString *inKeyPath, NSString
return([NSString stringWithFormat:@"%x:%x:%@:%@", inObserver, inTarget, inKeyPath, inIdentifier]);
}

static NSString *theHelpersKey = @"NSObject_KVOBlockNotificationExtensions_Helpers";

#pragma mark -

@implementation NSObject (NSObject_KVOBlockNotificationCenterExtensions)
@implementation NSObject (NSObject_KVOBlockNotificationExtensions)

- (void)addObserver:(NSObject *)observer handler:(KVOBlock)inHandler forKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions identifier:(id)inIdentifier
{
NSAssert(inHandler != NULL, @"No block");
NSAssert(inKeyPath != NULL, @"No key path");

static NSString *theHelpersKey = @"NSObject_KVOBlockNotificationCenterExtensions_Helpers";

NSMapTable *theHelpers = objc_getAssociatedObject(observer, theHelpersKey);
if (theHelpers == NULL)
{
Expand All @@ -78,12 +78,20 @@ - (void)addObserver:(NSObject *)observer handler:(KVOBlock)inHandler forKeyPath:
[self addObserver:theHelper forKeyPath:inKeyPath options:inOptions context:self];
}

- (void)removeObserver:(NSObject *)observer handler:(KVOBlock)inHandler forKeyPath:(NSString *)keyPath
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)inKeyPath identifier:(id)inIdentifier
{
// TODO
//NSAssert(inKeyPath != NULL, @"No key path");
//
//[[CKVOBlockNotificationCenter instance] removeKVOBlockForKeyPath:inKeyPath target:self identifier:inIdentifier];
id theKey = KeyForTarget(observer, self, inKeyPath, inIdentifier);

NSMapTable *theHelpers = objc_getAssociatedObject(observer, theHelpersKey);

CKVOBlockNotificationHelper *theHelper = [theHelpers objectForKey:theKey];

if (theHelper != NULL)
{
[self removeObserver:theHelper forKeyPath:inKeyPath];
//
[theHelpers removeObjectForKey:theKey];
}
}

@end
19 changes: 19 additions & 0 deletions Test/CTester.h
@@ -0,0 +1,19 @@
//
// CTester.h
// Test
//
// Created by Jonathan Wight on 06/30/10.
// Copyright 2010 toxicsoftware.com. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CTester : NSObject {
NSString *testValue;
}

@property (readwrite, nonatomic, retain) NSString *testValue;

- (void)test;

@end
33 changes: 33 additions & 0 deletions Test/CTester.m
@@ -0,0 +1,33 @@
//
// CTester.m
// Test
//
// Created by Jonathan Wight on 06/30/10.
// Copyright 2010 toxicsoftware.com. All rights reserved.
//

#import "CTester.h"

#import "CKVOBlockNotificationCenter.h"

@implementation CTester

@synthesize testValue;

- (void)test
{
__block NSString *theValue = NULL;
KVOBlock theBlock = ^(NSString *keyPath, id object, NSDictionary *change, id identifier) {
theValue = [change objectForKey:@"new"];
};

[self addObserver:self handler:theBlock forKeyPath:@"testValue" options:NSKeyValueObservingOptionNew identifier:@"FOO"];

NSLog(@"%@", theValue);
self.testValue = @"New Value";
NSLog(@"%@", theValue);

[self removeObserver:self forKeyPath:@"testValue" identifier:@"FOO"];
}

@end
2 changes: 0 additions & 2 deletions Test/English.lproj/InfoPlist.strings

This file was deleted.

0 comments on commit f81c222

Please sign in to comment.