【发布时间】:2018-01-09 11:36:15
【问题描述】:
我有一个 json data,它提供了以下信息:
let data = [
{
"QuestionTitle" : "Entomology is the science that studies",
"Id" : 205,
"Options" : [
{ "Option" : "Insects", "Id" : 810 },
{ "Option" : "The origin and history of technical and scientific terms", "Id" : 811 },
{ "Option" : "The formation of rocks", "Id" : 812 },
{ "Option" : "Behavior of human beings", "Id" : 809 }
]
},
{
"QuestionTitle" : "A train running at the speed of 60 km\/hr crosses a pole in 9 seconds. What is the length of the train?",
"Id" : 199,
"Options" : [
{ "Option" : "120 metres", "Id" : 785 },
{ "Option" : "324 metres", "Id" : 787 },
{ "Option" : "180 metres", "Id" : 786 },
{ "Option" : "150 metres", "Id" : 788 }
]
}
]
我正在使用 swiftyjson。我想使用 nsuserdefaults 保存整个数组。
GlobalVar.defaults.set(json, forKey: "questionArray")
GlobalVar.defaults.synchronize()
但是,我得到一个错误
“[用户默认值] 尝试设置非属性列表对象”。
请帮助我是 swift 的新手。我还检查了其他类似的问题,但似乎不起作用。
【问题讨论】:
-
试试 setObject(json.object, forKey: "questionArray")
-
您只能将对象保存到
UserDefaultsproperty list类型。为了保存自定义类/对象,您需要通过归档将其转换为NSData。见this answer
标签: swift alamofire swifty-json