-3

I have tried all solutions like try catch. But not able to solve this error. Please help me out, I am new to ios.

func apicalling () {

    let headers = [
        "content-type": "application/json",
        "cache-control": "no-cache",
        "postman-token": "7adebcbe-18b4-d2a7-2159-2fbcaea27edd"
    ]
    let parameters = [
        "customerID": "1",
        "listType": "2"
    ]

    let postData = JSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)



    var request = NSMutableURLRequest(url: NSURL(string: "http://exp.php")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.HTTPBody = postData

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse)
        }
    })

    dataTask.resume()

       }

The error in this below line :

let postData = JSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)

Please help me out.

Thanks.

mack
  • 603
  • 2
  • 9
  • 22

2 Answers2

0

In swift 3, it will be:

do {
  let data = try JSONSerialization.data(withJSONObject: parameters, options:[])
  // do your works
} catch {
  print("Got error: \(error)")
}

The API document is here: https://developer.apple.com/reference/foundation/jsonserialization/1413636-data

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
0

Since Swift 3, you need to add do, with try and catch, also the error argument got deleted, since it will throw now error, because it will get caught.

func apicalling () {

    let headers = [
        "content-type": "application/json",
        "cache-control": "no-cache",
        "postman-token": "7adebcbe-18b4-d2a7-2159-2fbcaea27edd"
    ]
    let parameters = [
        "customerID": "1",
        "listType": "2"
    ]
    do {
        let postData = try JSONSerialization.data(withJSONObject: parameters, options :[])
        let request = NSMutableURLRequest(url: NSURL(string: "http://exp.php")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
        request.httpMethod = "POST"
        request.allHTTPHeaderFields = headers
        request.httpBody = postData

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            if (error != nil) {
                print(error)
            } else {
                let httpResponse = response as? HTTPURLResponse
                print(httpResponse)
            }
        })

        dataTask.resume()
    } catch {
        print("JSON serialization failed:  \(error)")
    }
}
David Seek
  • 16,783
  • 19
  • 105
  • 136