我想我的字典迅速轉換成JSON字符串,但得到奇怪的崩潰:「無效的類型在JSON寫(_SwiftValue)」崩潰:轉換字典JSON字符串在3斯威夫特說</p> <p>終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因
我的代碼:
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)
}
請幫幫我!
問候。
「我的代碼」來自https://stackoverflow.com/a/29625483/2227743 – Moritz