无法使用类型的参数列表调用 'dataTask'(使用:NSMutableRequest,...)

Cannot invoke 'dataTask' with an argument list of type (with: NSMutableRequest, ...)

func performGetRequest(_ targetURL: URL!, completion: @escaping (_ data: Data?, _ HTTPStatusCode: Int, _ error: NSError?) -> Void) {
    let request = NSMutableURLRequest(url: targetURL)
    request.httpMethod = "GET"

    let sessionConfiguration = URLSessionConfiguration.default

    let session = URLSession(configuration: sessionConfiguration)

    let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
        DispatchQueue.main.async(execute: { () -> Void in
            completion(data, (response as! HTTPURLResponse).statusCode, error)
        })
    })

    task.resume()
}

我在这里尝试使用 appCoda 的演示来集成 YouTube API。但由于此方法错误而无法进一步移动:

无法使用类型为“(with: NSMutableURLRequest, completionHandler:(Data?, URLResponse..)'

的参数列表调用 'dataTask'

试试这个 -

func performGetRequest(_ targetURL: URL!, completion: @escaping (_ data: Data?, _ HTTPStatusCode: Int, _ error: NSError?) -> Void) {
    var request = URLRequest(url: targetURL)
    request.httpMethod = "GET"

    let sessionConfiguration = URLSessionConfiguration.default

    let session = URLSession(configuration: sessionConfiguration)

    let task = session.dataTask(with: request) { (data, response, error) in
        DispatchQueue.main.async(execute: { () -> Void in
            completion(data, (response as! HTTPURLResponse).statusCode, error)
        })
    }

    task.resume()
}

使用 URLRequest 代替 NSMutableURLRequest

func performGetRequest(_ targetURL: URL!, completion: @escaping (_ data: Data?, _ HTTPStatusCode: Int, _ error: NSError?) -> Void) {
        var request = URLRequest(url: targetURL)
        request.httpMethod = "GET"

        let sessionConfiguration = URLSessionConfiguration.default

        let session = URLSession(configuration: sessionConfiguration)


        let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
            DispatchQueue.main.async(execute: { () -> Void in
                completion(data, (response as! HTTPURLResponse).statusCode, error)
            })
        } as! (Data?, URLResponse?, Error?) -> Void)

        task.resume()
    }

您可以使用 URLRequest 代替 NSMutableURLRequest:

public func getData(withApiEndpoint apiURL: String, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) {
        var request = URLRequest(url: URL(string: apiURL)!)
        for (key, value) in self.getDefaultHeaders() {
            request.setValue(value, forHTTPHeaderField: key)
        }

        request.httpMethod = String.kGet

        if self.networkSession == nil {
            self.networkSession = URLSession(configuration: getSharedConfiguration(self.isIncognito))
        }
        self.networkDataTask = self.networkSession?.dataTask(with: request as URLRequest) { (data, response, error) in
            if error != nil {
                debugPrint(error!.localizedDescription)
            }
            completionHandler(data, response, error)
        }
        self.networkDataTask?.resume()
    }