Skip to content

Instantly share code, notes, and snippets.

@jyliang
Last active April 13, 2016 14:36
Show Gist options
  • Save jyliang/10207372 to your computer and use it in GitHub Desktop.
Save jyliang/10207372 to your computer and use it in GitHub Desktop.
KVO
static void * const MyClassKVOContext = (void*)&MyClassKVOContext;
- (void)addContext {
[object addObserver:self forKeyPath:NSStringFromSelector(@selector(property)) options:0 context:MyClassKVOContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == MyClassKVOContext) {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(property))]) {
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(isFinished))]) {
if ([object isFinished]) {
@try {
[object removeObserver:self forKeyPath:NSStringFromSelector(@selector(isFinished))];
}
@catch (NSException * __unused exception) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment