尝试设置float
的属性时,我一直收到错误。
我的Download
类NSObject
有一个原始float
。当类下载更新进度所需的文件时,将从NSURLSessionDownloadTask
委托方法调用该类。
虽然我不断收到错误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:]:发送到实例的无法识别的选择器 0x7fbec1cbfee0
由于未捕获的异常而终止应用 ' NSInvalidArgumentException',原因:' - [__ NSCFString setProgress:]: 无法识别的选择器发送到实例0x7fbec1cbfee0'第一次投掷 调用堆栈:
0 CoreFoundation __exceptionPreprocess + 165
1 libobjc.A.dylib objc_exception_throw + 48
2 CoreFoundation - [NSObject(NSObject)doesNotRecognizeSelector:] + 205
3 CoreFoundation 转发 + 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基金会_NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK_ + 7
答案 0 :(得分:1)
查看您的MainManagerViewController。您正在将setProgress发送到NSString *而不是Download对象。可能是MainManagerViewController URLSession的参数之一:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite
答案 1 :(得分:0)
这种错误通常是由于对象的重新分配而发生的。在您的代码示例中,Download
对象在您设置进度值后立即被释放。如果你没有对这个对象的强引用,那么指向它的指针就变得无效,因此可以指向任何其他对象,在你的情况下是一些随机的NSString
。
Download * download = [[Download alloc] initWithUrl@"SomeUrl"];
download.progress = (float)(totalBytesWritten)/(float)totalBytesExpectedToWrite;
// Download object deallocated