当 URLSessionDownload 任务失败时,从委托方法中接收到的错误对象中获取 resumeData

Get resumeData from error object received in delegate method when URLSessionDownload task fails

我已经实现了 URLSessionDownloadDelegate,每当我的 downloadTask 失败时,我都会在委托的回调方法之一中收到一个错误对象。当我附加调试器并将此错误对象打印到控制台时,我可以看到它包含 ResumeData 对象。但是我无法使用错误对象在代码中访问它。

这是控制台输出:

(lldb) po error.debugDescription "Optional(Error Domain=NSURLErrorDomain Code=-999 \"cancelled\" UserInfo={NSErrorFailingURLStringKey=https://mysampleurl, NSErrorFailingURLKey=https://mysampleurl, NSURLSessionDownloadTaskResumeData=<3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 . . . 692c2031 37204a75 6e203230 31362031 373a3433 3a303120 474d543c 2f737472 696e673e 0a3c2f64 6963743e 0a3c2f70 6c697374 3e0a>, NSLocalizedDescription=cancelled})"

我想从上面的错误对象输出中访问 "NSURLSessionDownloadTaskResumeData"

方法定义为:

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

我最近在处理这个需求,我创建了一个 class : https://github.com/skdevil/PrakrstaFileDownloader

这是您获取简历数据的方式:

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        if error != nil{
            let errorString = (error! as NSError).userInfo
            if let resumeData = errorString["NSURLSessionDownloadTaskResumeData"] as? Data, let urlKey = errorString["NSErrorFailingURLStringKey"] as? String {

            }
        }
    }