(URL )' to expected argument type ' 参数'"?-6ren"> (URL )' to expected argument type ' 参数'"?-我目前正在将我的代码库更新到 Swift 3.0。我正在使用 Alamofire。我不得不将 Alamofire 更新到 4.0 ( Alamofire git repo )。我有一种下载媒体(视频)-6ren">
gpt4 book ai didi

ios - Swift 3.0 迁移后 Alamofire 错误 : "Cannot convert value of type ' (URL, HTTPURLResponse)-> (URL )' to expected argument type ' 参数'"?

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

我目前正在将我的代码库更新到 Swift 3.0。我正在使用 Alamofire。我不得不将 Alamofire 更新到 4.0 ( Alamofire git repo )。我有一种下载媒体(视频)的方法,在迁移之前它工作得很好。使用 Xcode 的迁移工具后,我遇到了这个错误:“无法将类型 '(URL, HTTPURLResponse)-> (URL)' 的值转换为预期的参数类型 'Parameters?',这是一个 [String: Any]。到底是什么是这个Parameters对象吗?为什么它会抛出错误?迁移之前的这段代码和现在的唯一区别是现在NSURL被URL替换了。任何帮助都会很棒,因为我在过去的3个小时里一直坚持这个。

let mediaSourceURI: String = media.sourceURI
var filePath: URL?
let destination: (URL, HTTPURLResponse) -> (URL) = {
(temporaryURL, response) in

if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first, let suggestedFilename = response.suggestedFilename {
filePath = directoryURL.appendingPathComponent("\(suggestedFilename)")
return filePath!
}
return temporaryURL
}

//ERROR BELOW: "destination" is highlighted, and says "Cannot convert value of type '(URL, HTTPURLResponse)-> (URL)' to expected argument type 'Parameters?"
RequestManager.mediaDownloadAlamofireManager.download(mediaSourceURI, method: .get, parameters: destination).response {
(request, response, data, error) -> Void in

self.completeOperation()

}

这是我一直试图用作引用的链接: Alamofire 4.0 Migration Guide ,特别是在参数编码协议(protocol)部分。

以下是 Alamofire 中更新的下载方法的语法:
enter image description here

//mediaDownloadAlamofireManager 代码,没有错误:
static let mediaDownloadAlamofireManager: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
let serverTrustPolicies: [String: ServerTrustPolicy] = [baseURL : .disableEvaluation]
let manager = SessionManager(
configuration: configuration,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
return manager
}()

最佳答案

1. 简单的错字

RequestManager.mediaDownloadAlamofireManager.download(mediaSourceURI,method: .get, parameters: destination)



RequestManager.mediaDownloadAlamofireManager.download(mediaSourceURI,method: .get, destination: destination)



2.DownloadFileDestination-Type 的变化
Alamofire 4.0 changed the typeDownloadFileDestination – 您调用的参数 目的地 .它从:

(URL, HTTPURLResponse) -> (URL)



(URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions)


因此,您还需要将方法更改为以下内容:
let destination: (URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions) = {
(temporaryURL, response) in

if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first, let suggestedFilename = response.suggestedFilename {
filePath = directoryURL.appendingPathComponent("\(suggestedFilename)")
return (filePath!, [.removePreviousFile, .createIntermediateDirectories])
}
return (temporaryURL, [.removePreviousFile, .createIntermediateDirectories])
}
请注意,您不再只是返回 url,而且还有 more control over how Alamofire stores files on the file system .只需指定您想要的选项,例如 removePreviousFile 或/和 createIntermediateDirectories。它们现在以 Tuple 的形式返回。 .

关于ios - Swift 3.0 迁移后 Alamofire 错误 : "Cannot convert value of type ' (URL, HTTPURLResponse)-> (URL )' to expected argument type ' 参数'"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39864321/

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