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

1.2k views Asked by At

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

Here I am trying a demo from appCoda to integrate YouTube API. But not able to move further because of this method error :

Cannot invoke 'dataTask' with an argument list of type '(with: NSMutableURLRequest, completionHandler:(Data?, URLResponse..)'

3

There are 3 answers

0
Abhishek Jain On BEST ANSWER

Try this -

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()
}
3
Abdelahad Darwish On

use URLRequest inseatd of 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()
    }
0
Devanshu Saini On

You can use URLRequest instead of 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()
    }