gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:18:55 24 4
gpt4 key购买 nike

如何在 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 - 在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用 suggestedDownloadDestination,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41136560/

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