2016-10-19 1 views
-3

try catch와 같은 모든 솔루션을 시도했습니다. 그러나이 오류를 해결할 수 없습니다. 제발 도와주세요, 나는 이오가 처음이에요.스위프트 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

스위프트 3 있기 때문에, 당신은 또한 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)") 
    } 
} 
관련 문제