Skip to content

Instantly share code, notes, and snippets.

@tatey
Created April 29, 2012 23:35
Show Gist options
  • Select an option

  • Save tatey/2554031 to your computer and use it in GitHub Desktop.

Select an option

Save tatey/2554031 to your computer and use it in GitHub Desktop.
With and without branching
@implementation Test1
- (id)init {
self = [super init];
if (self) {
[self addObserver:self forKeyPath:@"property1" options:0 context:NULL];
[self addObserver:self forKeyPath:@"property2" options:0 context:NULL];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"property1"]) {
// Do something.
} else if ([keyPath isEqualToString:@"property2"]) {
// Do something.
}
}
@end
@implementation Test2
- (id)init {
self = [super init];
if (self) {
[self addObserver:self forKeyPath:@"property1" options:0 context:@selector(property1DidChange:)];
[self addObserver:self forKeyPath:@"property2" options:0 context:@selector(property2DidChange:)];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[self performSelector:(SEL)context withObject:change];
}
- (void)property1DidChange:(NSObject *)change {
// Do something.
}
- (void)property2DidChange:(NSObject *)change {
// Do something.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment