-3

Iam getting an error while i try to send the POST request in swift 3. Any one please suggest me the correct syntax for URLSession.shared method in swift 3. this is what i tried. iam new here.

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

            if error != nil{
                print("error");
                return
            }

            do{
                let myjson = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                if let parsejson = myjson{
                    var msg: String!
                    msg = parsejson["message"] as! String?
                    print(msg)
                }catch error  {
                print ("")

                }
            }
            })
        task.resume().   
ohm patel
  • 13
  • 5
  • you must use Alamofire, because it is effective and you should achieve it easily – Khawar Islam Jan 03 '18 at 17:48
  • 1
    @ohm Patel what have you tried , what have you searched? This can be simply googled. – Tushar Sharma Jan 03 '18 at 17:49
  • 4
    Post the actual code you have from your app in a code block as part of the question, and describe in detail the error you get. – David S. Jan 03 '18 at 18:07
  • @DavidS. please check the uploaded code. any corrections and suggetions will be appreciated. – ohm patel Jan 05 '18 at 10:44
  • Change print("error") to print("error: \(error)") and see what the error is. My guess if that line is firing is that you have the URL wrong or the server isn't running. – David S. Jan 05 '18 at 13:38
  • @DavidS. The error is in URLSession.shared line and the error says "ambigous reference to member 'dataTask(with: conpletionHandler:)" and anoter error is in "catch error " statement says " expected expression" – ohm patel Jan 06 '18 at 03:01
  • @DavidS. Can you suggest me the changes i should make to my code – ohm patel Jan 09 '18 at 16:13

1 Answers1

0

Here's working URLSession.shared code. I don't have your URL so I used one that is online, free, and produces JSON:

let someURL = URL(string:"https://jsonplaceholder.typicode.com/posts/2")!
let request = URLRequest(url: someURL)

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
    if error != nil {
        print("error")
        return
    }
    guard let data = data else {
        print("No data")
        return
    }

    do {
        if let myjson = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? Dictionary<String,Any> {
            if let title = myjson["title"] {
                print("Title was \"\(title)\"")
            }
        }
    } catch {
        print("Error parsing JSON: \(error)")
    }

}
task.resume()

This outputs Title was "qui est esse" for me.

David S.
  • 6,567
  • 1
  • 25
  • 45
  • Iam getting an error with this code. it says ` Invalid conversion from throwing function of type '(_, _, _) throws -> ()' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void' ` – ohm patel Jan 10 '18 at 16:55
  • iam getting an error mentioned above in comment. Do you have solution for this? – ohm patel Jan 16 '18 at 12:30
  • The code posted works perfectly in the latest Xcode. Maybe you're using an old version. – David S. Jan 17 '18 at 03:53
  • Yes Iam using xcode 8. But I think it should work on this version as well. – ohm patel Jan 17 '18 at 04:04
  • Old Xcode, old Swift version. Swift is an evolving language. What version of Swift are you using in your project? – David S. Jan 17 '18 at 14:09
  • With Xcode 9 and Swift 3.2 this worked with zero code changes. Not sure what you are doing wrong. I can't re-install Xcode 8. – David S. Jan 17 '18 at 14:27
  • Ohkk ..Thank you so much for the help. It means a lot. Thank you once again. – ohm patel Jan 17 '18 at 14:34