【问题标题】:Swift Argument labels '(_:, destination:)' do not match any available overloadsSwift 参数标签 '(_:, destination:)' 不匹配任何可用的重载
【发布时间】:2018-05-14 16:47:33
【问题描述】:

我将项目转换为 Swift 4.1 并在尝试重新制作时出错

 private func download(downloadable: Downloadable, destination: Request.DownloadFileDestination) -> Request {
        var downloadTask: URLSessionDownloadTask!
        switch downloadable {
        case .Request(let request):
            dispatch_sync(queue) {
                downloadTask = self.session.downloadTaskWithRequest(request)
            }
        case .ResumeData(let resumeData):
            dispatch_sync(queue) {
                downloadTask = self.session.downloadTaskWithResumeData(resumeData)
            }
        }
        let request = Request(session: session, task: downloadTask)
        if let downloadDelegate = request.delegate as? Request.DownloadTaskDelegate {
            downloadDelegate.downloadTaskDidFinishDownloadingToURL = { session, downloadTask, URL in
                return destination(URL, downloadTask.response as! NSHTTPURLResponse)
            }
        }
        delegate[request.delegate.task] = request.delegate
        if startRequestsImmediately {
            request.resume()
        }
        return request
    }

public func download(
    method: Method,
    _ URLString: URLStringConvertible,
    parameters: [String: AnyObject]? = nil,
    encoding: ParameterEncoding = .URL,
    headers: [String: String]? = nil,
    destination: Request.DownloadFileDestination)
    -> Request
{
    let mutableURLRequest = URLRequest(method: method, URLString, headers: headers)
    let encodedURLRequest = encoding.encode(URLRequest: mutableURLRequest, parameters: parameters).0
    return download(encodedURLRequest, destination: destination)
}

当我打电话时

错误:

参数标签 '(_:, destination:)' 不匹配任何可用的重载

【问题讨论】:

  • 你的意思是调用函数download1而不是download
  • 错误很明显。您的 download 函数的参数与您尝试传入的参数不同。也许您打算调用 download1
  • 是的,我解决了问题

标签: swift


【解决方案1】:

download 函数都没有在错误中显示的签名。

您可以将第一个 download 称为:

let result = download(downloadable: someValue, destination: someOtherValue)

或者您将第二个download 称为:

let result = download(method: someMethod, someURLString, destination: someDestination)

当然,第二次调用也可以有一个或多个可选参数。

你很可能想要改变:

return download(encodedURLRequest, destination: destination)

到:

return download(downloadable: encodedURLRequest, destination: destination)

这假定encodedURLRequest 的类型是Downloadable

【讨论】:

  • 当我打电话给return download(downloadable: encodedURLRequest, destination: destination) 时,它会给我Cannot convert value of type 'NSMutableURLRequest' to expected argument type 'Manager.Downloadable'
  • 在我回答的最后一句话中提到了这一点。您需要将正确的类型传递给每个参数。
猜你喜欢
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2020-01-07
  • 1970-01-01
相关资源
最近更新 更多