[英]Using KVO to track NSProgress
I am using KVO to track the progress of a file that is being received: 我正在使用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];
});}
and 和
- (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];
}
I would like to use this to update a UIprogressView... But the code is crashing at this line and I don't get why: 我想用它来更新UIprogressView ...但是代码在这一行崩溃了,我不明白为什么:
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
EDIT: 编辑:
This is the error 这是错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<ViewController1: 0x15b57000>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
EDIT: 编辑:
If I remove the super observeValueForKeyPath:keyPath..., the app doesn't crash, but the NSLog(@"Fraction Complete: %@ always reports the value "0". 如果我删除超级watchValueForKeyPath:keyPath ...,应用程序不会崩溃,但是NSLog(@“ Fraction Complete:%@总是报告值” 0“。
Does your superclass have observeValueForKeyPath:ofObject:change:context: implemented? 您的超类是否实现了watchValueForKeyPath:ofObject:change:context :? You can do an if check on that:
您可以对此进行if检查:
if ([super respondsToSelector:@selector(observeValueForKeyPath:ofObject:change:context:){
NSLog(@"Yeah, I'm here matey!");
} else {
NSLog(@"Uh oh said the selector ghost");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.