gpt4 book ai didi

ios - 在 NSObject 中设置 float 会抛出 doesNotRecognizeSelector 错误

转载 作者:行者123 更新时间:2023-11-29 00:45:54 26 4
gpt4 key购买 nike

我在尝试设置float 的属性时不断收到错误消息。

我有一个 Download 类的 NSObject,它有一个原始的 float。当类下载更新进度所需的文件时,从 NSURLSessionDownloadTask 委托(delegate)方法调用该类。

虽然我一直收到错误 setProgress: unrecognized selector sent to instance

知道为什么吗?

我下载NSObject类.h

@interface Download : NSObject {

}
@property (nonatomic, strong) NSString* url;
@property (assign) float progress;
- (id) initWithUrl:(NSString*)url;

@end

我下载NSObject类.m

#import "Download.h"

@implementation Download

- (id) initWithUrl:(NSString*)url {
if (self = [super init]) {
self.url = url;
}

return self;

}
@end

被调用的方法

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
{
//OTHER STUFF

Download * download = [[Download alloc] initWithUrl@"SomeUrl"];
download.progress = (float)(totalBytesWritten)/(float)totalBytesExpectedToWrite;

}

错误

[__NSCFString setProgress:]: unrecognized selector sent to instance 0x7fbec1cbfee0

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setProgress:]: unrecognized selector sent to instance 0x7fbec1cbfee0' First throw call stack:

0 CoreFoundation __exceptionPreprocess + 165

1 libobjc.A.dylib objc_exception_throw + 48

2 CoreFoundation -[NSObject(NSObject) doesNotRecognizeSelector:] + 205

3 CoreFoundation forwarding + 970

4 CoreFoundation _CF_forwarding_prep_0 + 120

5 DownloadManger -[MainManagerViewController URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:] + 420

6 CFNetwork __113-[NSURLSession delegate_downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:completionHandler:]_block_invoke + 50

7 Foundation _NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK_ + 7

最佳答案

查看您的 MainManagerViewController。您将 setProgress 发送到 NSString * 而不是 Download 对象。可能是 MainManagerViewController 的参数之一 URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite

关于ios - 在 NSObject 中设置 float 会抛出 doesNotRecognizeSelector 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38592010/

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