gpt4 book ai didi

ios - 使用 KVO 跟踪 NSProgress

转载 作者:行者123 更新时间:2023-11-29 13:01:44 26 4
gpt4 key购买 nike

我正在使用 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”。

最佳答案

您的父类(super class)是否实现了 observeValueForKeyPath:ofObject:change:context: ?你可以做一个 if 检查:

if ([super respondsToSelector:@selector(observeValueForKeyPath:ofObject:change:context:){
NSLog(@"Yeah, I'm here matey!");
} else {
NSLog(@"Uh oh said the selector ghost");
}

关于ios - 使用 KVO 跟踪 NSProgress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com