URLSession trowing an NSURLSession exception

660 views Asked by At

Swift 3 on Xcode 8β3.

Any ideas why the code below is throwing the error (further) below, particularly given that I am using URLSession not NSURLSession?

CODE

public func updateCurrentConditions() {

    let session = URLSession()
    let url     = URL(string: "http://api.wunderground.com/api/\(key)/conditions/51.32,-1.0.json")!
    let loadDataTask = session.dataTask(with: url, completionHandler: self.dataHandler) 
    loadDataTask.resume()
}

private func dataHandler(data: Data?, response: URLResponse?, error: NSError?) -> Void {
    print("dataHandler executed.")
}

ERROR

failed: caught "NSInvalidArgumentException", "-[NSURLSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x7fac89569f00"

1

There are 1 answers

5
OOPer On BEST ANSWER

Have you tried using shared URLSession?

let session = URLSession.shared

The default initializer of URLSession is undefined, so it may be creating something weird...