gpt4 book ai didi

swift - 将 swifty json 数组保存为用户默认值

转载 作者:搜寻专家 更新时间:2023-10-30 22:31:51 26 4
gpt4 key购买 nike

我有一个 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()

但是,我得到一个错误

"[User Defaults] Attempt to set a non-property-list object".

请协助我是 swift 的新手。我也检查了其他类似的问题,但似乎不起作用。

最佳答案

您不能将 SwiftyJSON 的自定义类型 JSON 保存到 UserDefaults 但您可以保存 raw 数组,因为反序列化的 JSON 集合类型符合属性列表.

只需在 JSON 对象上调用 arrayObject:

GlobalVar.defaults.set(json.arrayObject, forKey: "questionArray")

关于swift - 将 swifty json 数组保存为用户默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45457523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com