【发布时间】:2017-03-05 17:59:24
【问题描述】:
我正在尝试将我的 swift 字典转换为 Json 字符串,但说起来却发生了奇怪的崩溃
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入 (_SwiftValue) 中的类型无效”
我的代码:
let jsonObject: [String: AnyObject] = [
"firstname": "aaa",
"lastname": "sss",
"email": "my_email",
"nickname": "ddd",
"password": "123",
"username": "qqq"
]
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
// here "jsonData" is the dictionary encoded in JSON data
let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
// here "decoded" is of type `Any`, decoded from JSON data
// you can now cast it with the right type
if let dictFromJSON = decoded as? [String:String] {
// use dictFromJSON
}
} catch {
print(error.localizedDescription)
}
请帮帮我!
问候。
【问题讨论】: