我正在使用KVO跟踪正在接收的文件的进度:
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);
dispatch_sync(dispatch_get_main_queue(), ^{
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
});}
和
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (object == progress) {
// Handle new fractionCompleted value
[progressBar setProgress:progress.fractionCompleted animated:YES];
NSLog(@"Fraction Complete: %@", [NSNumber numberWithDouble:progress.fractionCompleted]);
return;
}
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
我想用它来更新UIprogressView ... 但是代码在这一行崩溃了,我不明白为什么:
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
编辑:
这是错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<ViewController1: 0x15b57000>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
编辑:
如果删除super observeValueForKeyPath:keyPath ...,应用程序不会崩溃,但NSLog(@“Fraction Complete:%@总是报告值”0“。
答案 0 :(得分:1)
你的超类是否有observeValueForKeyPath:ofObject:change:context:已实现? 你可以检查一下:
if ([super respondsToSelector:@selector(observeValueForKeyPath:ofObject:change:context:){
NSLog(@"Yeah, I'm here matey!");
} else {
NSLog(@"Uh oh said the selector ghost");
}