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

时间:2017-08-28 06:55:50

标签: swift nsurlsession nsurlsessiondatatask nsurlsessionconfiguration

enter image description here

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。但由于此方法错误,无法进一步移动:

无法使用类型为'的参数列表调用'dataTask'(with:NSMutableURLRequest,completionHandler:(Data?,URLResponse ..)'

3 个答案:

答案 0 :(得分:2)

您可以使用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()
    }

答案 1 :(得分:1)

试试这个 -

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()
}

答案 2 :(得分:1)

使用NSMutableURLRequest

的URLRequest
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()
    }