2016-10-19 7 views
-3

私はtry catchのようなすべてのソリューションを試しました。しかし、このエラーを解決することはできません。私を助けてください、私は新しいです。行の下、この中Swift 3:余分な引数 'error'

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() 

     } 

エラー:

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

私を助けてください。

ありがとうございました。迅速な3では

答えて

0

、あなたはそれがするので、error引数は削除されたまた、do, with try and catchを追加する必要があります捕まえられるので、今すぐスローしてください。

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)") 
    } 
} 
関連する問題