gpt4 book ai didi

ios - NSURLSessionDownloadDelegate 协议(protocol)的错误信息

转载 作者:行者123 更新时间:2023-11-29 12:18:11 25 4
gpt4 key购买 nike

我实际上是在处理下载/上传文件并跟踪进度。它运作良好。但是,我只是想知道如果出现问题怎么办,而且我在该委托(delegate)中没有看到任何处理错误的方法。以下是 NSURLSessionDownloadDelegate 中的方法:

/*
* Messages related to the operation of a task that writes data to a
* file and notifies the delegate upon completion.
*/
@protocol NSURLSessionDownloadDelegate <NSURLSessionTaskDelegate>

/* Sent when a download task that has completed a download. The delegate should
* copy or move the file at the given location to a new location as it will be
* removed when the delegate message returns. URLSession:task:didCompleteWithError: will
* still be called.
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;

@optional
/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;

/* Sent when a download has been resumed. If a download failed with an
* error, the -userInfo dictionary of the error will contain an
* NSURLSessionDownloadTaskResumeData key, whose value is the resume
* data.
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes;

@end

只是想知道如何返回错误消息,谢谢。

最佳答案

让您的代表符合 NSURLSessionTaskDelegate并实现:

- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error

EDIT 以进一步澄清,请参阅 the doc for NSURLSessionDownloadDelegate ,请注意靠近顶部的地方:

In addition to these methods, be sure to implement the methods in the NSURLSessionTaskDelegate and NSURLSessionDelegate protocols to handle events common to all task types and session-level events, respectively.

想法是 url session 的委托(delegate)可以符合任何这些协议(protocol)。您正在寻找的方法,即指示错误的方法,只是比下载任务更通用,它可能发生在任何类型的 session 任务上,因此他们将其放在更抽象的协议(protocol)中。

关于ios - NSURLSessionDownloadDelegate 协议(protocol)的错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31481981/

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