forked from mikeash/MAKVONotificationCenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTester.m
51 lines (36 loc) · 1.23 KB
/
Tester.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Tester.m
// MAKVONotificationCenter
//
// Created by Michael Ash on 10/15/08.
//
#import "Tester.h"
#import "MAKVONotificationCenter.h"
@implementation Tester
- (void)testDictionary
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"foo", @"key",
nil];
[dict addObserver:self forKeyPath:@"key" selector:@selector(_observeValueForKeyPath:ofObject:change:userInfo:) userInfo:@"userInfo" options:NSKeyValueObservingOptionNew];
[dict setObject:@"bar" forKey:@"key"];
NSAssert(_triggered, @"failed to trigger");
[dict removeObserver:self keyPath:@"key" selector:@selector(_observeValueForKeyPath:ofObject:change:userInfo:)];
_triggered = NO;
[dict setObject:@"foo" forKey:@"key"];
NSAssert(!_triggered, @"triggered after deregistering");
}
- (void)_observeValueForKeyPath:(NSString *)keyPath ofObject:(id)target change:(NSDictionary *)change userInfo:(id)userInfo
{
NSLog(@"%s: %@ changed %@ dictionary %@ info %@", __func__, target, keyPath, change, userInfo);
_triggered = YES;
}
@end
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Tester *tester = [[Tester alloc] init];
[tester testDictionary];
[pool release];
return 0;
}