###前提・実現したいこと
SwiftでDictionary型で保存したデータをJSONに書き込んで保存したい
↓Dictionaryのデータ
["Priority": 1, "notificationTime": null, "TaskName": 今日中にやること1], ["Priority": 2, "notificationTime": null, "TaskName": 今日中にやること2], ["Priority": 3, "notificationTime": null, "TaskName": 今日中にやること3], ["Priority": 4, "notificationTime": null, "TaskName": 今日中にやること4], ["Priority": 5, "notificationTime": null, "TaskName": 今日中にやること5], ["Priority": 6, "notificationTime": null, "TaskName": 今日中にやること6]
###発生している問題・エラーメッセージ
↑をひとまとめにして、その上で別のデータとDictionaryに入れ、JSONにシリアライズしたいが、元の書き方から変わってしまっているのか、下のようなエラーになる
2017-08-10 15:51:18.147895+0900 Spicker[3861:1487060] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)' *** First throw call stack: (0x184e30d5c 0x184344528 0x184e30ca4 0x1858a9684 0x1858ab8fc 0x1007ced44 0x10061d688 0x10061db5c 0x1858aaec4 0x1858ab8fc 0x184d541c4 0x1858aaec4 0x1858ab8fc 0x1007ced44 0x10061d688 0x10061db5c 0x1858aaec4 0x1858a9334 0x1858aa384 0x10005e690 0x10005f5a4 0x18e993310 0x18e993290 0x18e97e050 0x18e992b84 0x18e9926a4 0x18e98db74 0x18e95efc8 0x18f27cc2c 0x18f27f09c 0x18f278208 0x184dd9570 0x184dd94f0 0x184dd8d78 0x184dd6950 0x184cf7558 0x186b7af84 0x18e9c2984 0x100064180 0x184819db4) libc++abi.dylib: terminating with uncaught exception of type NSException
その際のデータをprintしたら、このような感じになっていました
["DataNum": 7, "Description": { 1 = { Priority = 1; TaskName = "\U4eca\U65e5\U4e2d\U306b\U3084\U308b\U3053\U3068\Uff11"; notificationTime = null; }; 2 = { Priority = 2; TaskName = "\U4eca\U65e5\U4e2d\U306b\U3084\U308b\U3053\U3068\Uff12"; notificationTime = null; }; 3 = { Priority = 3; TaskName = "\U4eca\U65e5\U4e2d\U306b\U3084\U308b\U3053\U3068\Uff13"; notificationTime = null; }; 4 = { Priority = 4; TaskName = "\U4eca\U65e5\U4e2d\U306b\U3084\U308b\U3053\U3068\Uff14"; notificationTime = null; }; 5 = { Priority = 5; TaskName = "\U30c6\U30b9\U30c8"; notificationTime = ""; }; 6 = { Priority = 6; TaskName = "\U4eca\U65e5\U4e2d\U306b\U3084\U308b\U3053\U3068\Uff15"; notificationTime = null; }; 7 = { Priority = 7; TaskName = "\U4eca\U65e5\U4e2d\U306b\U3084\U308b\U3053\U3068\Uff16"; notificationTime = null; }; }]
###該当のソースコード
Swift
1let newData_NS = NSDictionary(dictionary: newData) 2 print(newData_NS) 3 4 var toJsonData:Dictionary<String, AnyObject> = [:] 5 toJsonData["DataNum"] = NSString(string: String(describing:DataNumber)) 6 toJsonData["Description"] = NSDictionary(dictionary: newData_NS) 7 print("Dictionaryデータの作成が完了しました") 8 9 print(toJsonData) 10 var json: String = "" 11 12 let jsonData = try? JSONSerialization.data(withJSONObject: toJsonData, options: JSONSerialization.WritingOptions()) 13 let jsonString = NSString(data: jsonData!, encoding: String.Encoding.utf8.rawValue) 14 print(jsonString) 15
###補足情報(言語/FW/ツール等のバージョンなど)
・Xcode9
・パーサーにはSwiftyJSONを使っていました(あまり関係ないとは思いますが・・・)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。