Created
December 23, 2010 04:36
-
-
Save atnan/752571 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NDVKVOMacros.h" | |
@implementation Foo | |
@synthesize bar; | |
- (id)init { | |
self = [super init]; | |
if (self != nil) { | |
OBSERVE_CHANGES_FOR_PROPERTIES(@"bar"); | |
} | |
return self; | |
} | |
- (void)barDidChangeWithOldValue:(id)oldValue | |
newValue:(id)newValue { | |
// Handle the new value | |
} | |
SYNTHESIZE_PROPERTY_CHANGE_OBSERVER(); | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NDVKVOMacros.h | |
// NDVKit | |
// | |
// Created by Nathan de Vries on 25/08/10. | |
// Copyright 2010 Nathan de Vries. All rights reserved. | |
// | |
#define OBSERVE_CHANGES_FOR_PROPERTIES(...) \ | |
\ | |
for (NSString* property in [NSArray arrayWithObjects:__VA_ARGS__, nil]) { \ | |
[self addObserver:self \ | |
forKeyPath:property \ | |
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) \ | |
context:NULL]; \ | |
} | |
#define SYNTHESIZE_PROPERTY_CHANGE_OBSERVER() \ | |
\ | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { \ | |
if ([[change valueForKey:NSKeyValueChangeKindKey] integerValue] == NSKeyValueChangeSetting) { \ | |
SEL changeSelector = NSSelectorFromString([keyPath stringByAppendingString:@"DidChange"]); \ | |
SEL changeSelectorWithValues = NSSelectorFromString([keyPath stringByAppendingString:@"DidChangeWithOldValue:newValue:"]); \ | |
\ | |
if ([self respondsToSelector:changeSelector]) { \ | |
[self performSelector:changeSelector]; \ | |
\ | |
} else if ([self respondsToSelector:changeSelectorWithValues]) { \ | |
id oldValue = [change valueForKey:NSKeyValueChangeOldKey] ?: nil; \ | |
id newValue = [change valueForKey:NSKeyValueChangeNewKey]; \ | |
\ | |
oldValue = (oldValue == [NSNull null]) ? nil : oldValue; \ | |
\ | |
[self performSelector:changeSelectorWithValues withObject:oldValue withObject:newValue]; \ | |
} \ | |
} \ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment