JSONSerialization Invalid type in JSON write (_SwiftValue)

前端 未结 10 927
情书的邮戳
情书的邮戳 2020-12-14 14:14

Why does the following code give me the error:

Invalid type in JSON write (_SwiftValue).

The error is thrown on this line:

10条回答
  •  春和景丽
    2020-12-14 14:26

    If you're using SwiftyJSON to access a JSON object, it's important to use the dictionaryObject property of the JSON (instead of using dictionaryValue, dictionary or nothing at all), because you will get this error (or a variation of it) otherwise. For example:

    guard let jsonDict = json.dictionaryObject else {
        return
    }
    
    let jsonData = try JSONSerialization.data(withJSONObject: jsonDict, options: [])
    

提交回复
热议问题