8

I'm trying to make a POST request in the Swift 3 development snapshot but for some reason, the call to NSURLSession.dataTask fails with the error in the Title.

Here is the code I am using:

import Foundation

var err: NSError?
var params: Dictionary<String, String>
var url: String = "http://notreal.com"

var request = NSMutableURLRequest(url: NSURL(string: url)!)
var session = NSURLSession.shared()

request.httpMethod = "POST"
request.httpBody = try NSJSONSerialization.data(withJSONObject: params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

var task = session.dataTask(with: request, completionHandler: {data, response, err -> Void in
    print("Entered the completionHandler")
})

task.resume()

The error is exactly:

testy.swift:19:12: error: ambiguous reference to member 'dataTask(with:completionHandler:)'
var task = session.dataTask(with: request, completionHandler: {data, response, err -> Void in
           ^~~~~~~
Foundation.NSURLSession:2:17: note: found this candidate
    public func dataTask(with request: NSURLRequest, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Swift.Void) -> NSURLSessionDataTask
                ^
Foundation.NSURLSession:3:17: note: found this candidate
    public func dataTask(with url: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Swift.Void) -> NSURLSessionDataTask

Can anyone tell me:

  1. Why it's giving me this error
  2. How to successfully make a POST request with custom parameters in the latest Swift development snapshot using Foundation only (I am unable to use other third party libraries under any circumstances)

Thanks!

Edit: I note that someone wrote a duplicate of this question subsequent to mine. The answer here is the better one.

Chuppa Chump
  • 301
  • 1
  • 2
  • 13
  • This is odd, I can't see why passing `NSMutableURLRequest` is ambiguous. It's not a subclass of `NSURL`, so IDK how it would think it could go in there. – Alexander May 30 '16 at 15:26
  • This is what I was thinking. It may be the case that it's a bug, as it is the development snapshot. – Chuppa Chump May 30 '16 at 15:47
  • You can try to disambiguate this call by assigning `dataTaskWithRequest:completionHandler:` to a variable with a type annotation specifying it's `(NSURL, (NSData?, NSURLResponse?, NSError?) -> Swift.Void) -> NSURLSessionDataTask` and then calling that closure – Alexander May 30 '16 at 15:59
  • 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) – SwiftDeveloper Sep 30 '16 at 13:51

2 Answers2

23

use URLRequest struct.

In Xcode8 will work fine:

import Foundation

// In Swift3, use `var` struct instead of `Mutable` class.
var request = URLRequest(url: URL(string: "http://example.com")!)
request.httpMethod = "POST"

URLSession.shared.dataTask(with: request) {data, response, err in
    print("Entered the completionHandler")
}.resume()

In addition, reason of this error is URLSession API has same name method, but each take a different argument .

So, API will be confused without explicit cast. I think this is API's naming mistake.

occurred this problem, following code:

let sel = #selector(URLSession.dataTask(with:completionHandler:))
Cœur
  • 37,241
  • 25
  • 195
  • 267
quesera2
  • 346
  • 5
  • 13
2

Please note that as of Xcode 8.0 release URLSession.shared() becomes property and not method so you will have to call it as URLSession.shared.dataTask(with:);

Sergey Kuznetsov
  • 518
  • 4
  • 10