我创建了一个名为VJSessionTask的自定义VJSessionTask,我刚刚添加了一些定制的东西,比如类型(enum)和自定义对象(id)。
@interface VJSessionTask : NSURLSessionDownloadTask
typedef enum types
{
LS, LSH, DL, UL, RM, TH
} type;
@property enum types type;
@property (strong, nonatomic) id customObject;
@property (strong, nonatomic) NSString *progressNotif;
@property (strong, nonatomic) NSString *doneNotif;
@property (strong, nonatomic) NSURL *tmpFile;
@end当我这么做的时候
VJSessionTask *taskSession = (VJSessionTask *)[self.prioritySession downloadTaskWithRequest:listFileRequest];
// init taskSession with its type
taskSession.type = LS;我知道这个错误:
-[__NSCFLocalDownloadTask setType:]: unrecognized selector sent to instance 0x1556198f0然后我来找你,因为我不明白或者我不知道怎么做.(预先谢谢你;)
发布于 2014-10-13 14:02:32
不幸的是,NSURLSessionTasks并不严格地说是可子类的。这一点很明显,系统可以对数据任务排队并返回一个NSCFLocalDownloadTask (大概意味着该任务将从缓存返回其内容)。
这样做的最佳方法是借鉴AFNetworking的体系结构决策,并让单独的taskDelegates监视单个任务所做的所有响应。然后,当您想查找与某个任务相关的数据时,可以查询您的taskDelegates字典。每个任务都有一个唯一的标识符,您可以使用该标识符来键入字典。
在AFNetworking中,您可以看到taskDelegate的定义如下:
@interface AFURLSessionManagerTaskDelegate : NSObject <NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
@property (nonatomic, weak) AFURLSessionManager *manager;
@property (nonatomic, strong) NSMutableData *mutableData;
@property (nonatomic, strong) NSProgress *progress;
@property (nonatomic, copy) NSURL *downloadFileURL;
@property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;
@property (nonatomic, copy) AFURLSessionTaskCompletionHandler completionHandler;
@end
@implementation AFURLSessionManagerTaskDelegate并随后检索如下:
- (AFURLSessionManagerTaskDelegate *)delegateForTask:(NSURLSessionTask *)task {
NSParameterAssert(task);
AFURLSessionManagerTaskDelegate *delegate = nil;
[self.lock lock];
delegate = self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)];
[self.lock unlock];
return delegate;
}有关更多信息,请参见这个职位
https://stackoverflow.com/questions/26341628
复制相似问题