swift3 – 设置Alamofire自定义目标文件名,而不是在Swift 3.0中使用suggestDownloadDestination

swift3 – 设置Alamofire自定义目标文件名,而不是在Swift 3.0中使用suggestDownloadDestination,第1张

概述如何在swift 3.0中编写以下代码段?以下语法在swift 2中 Alamofire.download(.POST, invoice.url,parameters:params, destination: { (url, response) -> NSURL in let pathComponent = response.suggestedFilename 如何在swift 3.0中编写以下代码段?以下语法在swift 2中
Alamofire.download(.POST,invoice.url,parameters:params,destination: { (url,response) -> NSURL in        let pathComponent = response.suggestedfilename        let fileManager = NSfileManager.defaultManager()        let directoryURL = fileManager.URLsForDirectory(.documentDirectory,inDomains: .UserDomainMask)[0]        let fileUrl = directoryURL.URLByAppendingPathComponent(pathComponent)        return fileUrl    })    .progress { bytesRead,totalBytesRead,totalBytesExpectedToRead in        print(totalBytesRead)        dispatch_async(dispatch_get_main_queue()) {            let progress = Double(totalBytesRead) / Double(totalBytesExpectedToRead)            completionHandler(progress,nil)        }    }    .responseString { response in        print(response.result.error)        completionHandler(nil,response.result.error)    }
在Swift 3中它就是这样的.
let parameters: Parameters = ["foo": "bar"]let destination: DownloadRequest.DownloadfileDestination = { _,_ in    let pathComponent = "yourfilename"    let documentsURL = fileManager.default.urls(for: .documentDirectory,in: .userDomainMask)[0]    let fileURL = documentsURL.appendpathComponent(pathComponent)    return (fileURL,[.removePrevIoUsfile,.createIntermediateDirectorIEs])}Alamofire.download(urlString,method: .get,parameters: parameters,enCoding: JsONEnCoding.default,to: destination)    .downloadProgress(queue: dispatchQueue.global(qos: .utility)) { progress in        print("Progress: \(progress.fractionCompleted)")    }    .valIDate { request,response,temporaryURL,destinationURL in        // Custom evaluation closure Now includes file URLs (allows you to parse out error messages if necessary)        return .success    }    .responseJsON { response in        deBUGPrint(response)        print(response.temporaryURL)        print(response.destinationURL)    }

有关详细信息,请查看Alamofire DocumentationAlamofire 4.0 Migration Guide.

总结

以上是内存溢出为你收集整理的swift3 – 设置Alamofire自定义目标文件名,而不是在Swift 3.0中使用suggestDownloadDestination全部内容,希望文章能够帮你解决swift3 – 设置Alamofire自定义目标文件名,而不是在Swift 3.0中使用suggestDownloadDestination所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047182.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存