0

I migrated my project (initially developed in Swift 2.2) to Swift 3 but I encounter an error with the following code:

    let url:URL = URL(string: url_to_request)!
    //let session = NSURLSession.sharedSession()
    let configuration = URLSessionConfiguration.default
    let session = Foundation.URLSession(configuration: configuration,
                               delegate: self,
                               delegateQueue:OperationQueue.main)

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
    request.timeoutInterval = 10

    let task = session.dataTask(with: request, completionHandler: {
        (
        data, response, error) in

        if(response == nil){
            print("erreur de connexion....")
            result = false
            callback(result)
        }

I have the following error

Ambiguous reference to member 'dataTask(with:CompletionHandler:)'

I've seen that in Swift3 we may use NSURLSession.sharedSession() but as I'm using a Customized session, it will not work as I'm expecting...

Any idea how can I solve it ?

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
tiamat
  • 879
  • 2
  • 12
  • 35
  • Possible duplicate of [Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug)](http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet) – vadian Nov 02 '16 at 12:50

1 Answers1

1

The compiler wants native URLRequest rather than NSMutableURLRequest

var request = URLRequest(url: url)
// request.httpMethod = "GET" <- GET is the default
request.cachePolicy = .reloadIgnoringLocalCacheData // <- renamed enum case
request.timeoutInterval = 10
vadian
  • 274,689
  • 30
  • 353
  • 361