2016-10-08 8 views
1

私はJSONSerializationためSIGABRエラーを取得...ここにここでは、コード(XCode8)スウィフト:JSONSerialization SIGABRTエラー

func makeHTTPPostRequest(path: String, body: String, onCompletion: @escaping ServiceResponse) { 
    var err: NSError? 
    let request = NSMutableURLRequest(url: NSURL(string: path)! as URL) 

    // Set the method to POST 
    request.httpMethod = "POST" 

    // Set the POST body for the request 

    request.setValue("application/json", forHTTPHeaderField: "Content-Type") 
    request.httpBody = try! JSONSerialization.data(withJSONObject: body, options: []) 

    let session = URLSession.shared 

    let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in 
     let json:JSON = JSON(data: data!) 
     onCompletion(json, err) 
    }) 
    task.resume() 
} 

は体変数の値である:{ "firstNameのは、": "追加"、 "電子メール":" asd "、" lastName ":" add "、" password ":" asdas "}

ご協力いただければ幸いです。あなたはSWIFT 3.0

let dataTsked = session.dataTask(with: URL(string: path)!) { (data, response, error) in 
    if (error != nil) { 

答えて

0

StringJSONObjectはなくタイプではありません。使用しようとしている関数には、DictionaryArrayのようなトップレベルのFoundationオブジェクトが必要です。

リクエストにボディデータを渡すだけで済みます。

ので、代わりの:

request.httpBody = try! JSONSerialization.data(withJSONObject: body, options: []) 

それは次のようになります。

request.httpBody = body.data(using: .utf8)! 

注:体は、エラーがJSONSerializationであるだけでなく、フォーマットJSON文字列

+0

する必要があります... –

1

bodyに構文を更新する必要が