-
-
Save jyliang/10207372 to your computer and use it in GitHub Desktop.
KVO
This file contains 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
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