Home How to solve this issue "Ambiguous reference to member 'dataTask(with:completionHandler:)' "
Reply: 1

How to solve this issue "Ambiguous reference to member 'dataTask(with:completionHandler:)' "

Bar Uncle
1#
Bar Uncle Published in 2018-02-14 09:26:22Z

This question already has an answer here:

  • Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug) 13 answers

How to solve this issue

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

    guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }

    let request = NSMutableURLRequest(url: requestUrl)
    request.httpMethod = "POST"

    let postString = "firstName=James&lastName=Bond"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request){ (data, response, error) in

        if error != nil
        {
            //print(error)
            return
        }

        //You can print out response object
        print("response = \(response)")

        //Print out response body
        let responseString = String(data: data, encoding: NSUTF8StringEncoding)
        print("response data = \(responseString)")

        var err: Error?
        var json = JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary

        if let parseJSON = json {
            var firstNameValue = parseJSON["firstName"] as? String
            print("first name value = \(firstNameValue)")
        }
    }
    task.resume()
}
Sh_Khan
2#
Sh_Khan Reply to 2018-02-14 10:11:20Z

Call should be like this , first parameter is of type URL not NSMutableURLRequest

  guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }

    var  request = URLRequest(url: requestUrl)
    request.httpMethod = "POST"

    let postString = "firstName=James&lastName=Bond"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request){ (data, response, error) in

        if error != nil
        {
            //print(error)
            return
        }


    }
    task.resume()

see here signature of available methods

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.357884 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO