Ali
Ali

Reputation: 323

Cannot invoke 'URLSession' with an argument list of type '(configuration: URLSessionConfiguration)'

Notice that i've tried so many solution like this and none worked.

here is screenshot and code;

func downloadItems() {

    let urlE: String = "http://alicelik.me/forios/service.php"
    guard let url = URL(string: urlE) else {
        print("Error: cannot")
        return
    }

    let configuration = URLSessionConfiguration.default
    var session = URLSession(configuration: configuration)

    let task = session.dataTask(with: url as URL){
        (data, response, error) in
        guard error == nil else {
            print("error calling GET on /todos/1")
            print(error)
        }
        return()
    }

    task.resume()

}

Here is URLSession func;

@nonobjc func URLSession(session: URLSessionConfiguration, dataTask: URLSessionDataTask, didReceiveData data: NSData) {
    self.data.append(data as Data);

}

@nonobjc func URLSession(session: URLSessionConfiguration, task: URLSessionTask, didCompleteWithError error: NSError?) {
    if error != nil {
        print("Failed to download data")
    }else {
        print("Data downloaded")
        self.parseJSON()
    }

}

note: *URLSession and *URLSessionConfiguration in func URLSession(session *) giving same error.

Upvotes: 3

Views: 1115

Answers (2)

Ali
Ali

Reputation: 323

Changing URLSession func will fix issue.

func downloadItems() {
    let url: NSURL = NSURL(string: urlPath)!
    var session = URLSession()
    let configuration = URLSessionConfiguration.default
    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    let task = session.dataTask(with: url as URL)
    task.resume()
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    self.data.append(data as Data)
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if error != nil {
        print("Failed to download data")
    } else {
        print("Data downloaded")
        self.parseJSON()
    }
}

Upvotes: 1

theCarrNation
theCarrNation

Reputation: 41

I had the same problem, solved it by:

var session = Foundation.URLSession(configuration: configuration)

Upvotes: 4

Related Questions