Created
June 21, 2010 07:14
-
-
Save atnan/446524 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
/* | |
* NSObjectPropertyObservers.h | |
* | |
* Created by Nathan de Vries on 12/03/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