本文介绍了将 swifty json 数组保存到用户默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 json data 它提供了以下信息:

i have a json data which gives the following information:

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 保存整个数组.

I am using swiftyjson. I want to save the entire array using nsuserdefaults.

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

但是,我收到一个错误

[用户默认值] 尝试设置非属性列表对象".

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

Please assist i am new to swift. I have also checked other similar questions but seems to not work.

推荐答案

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

You cannot save SwiftyJSON's custom type JSON to UserDefaults but you can save the raw array because a deserialized JSON collection type is property list compliant.

只需在 JSON 对象上调用 arrayObject:

Just call arrayObject on the JSON object:

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

这篇关于将 swifty json 数组保存到用户默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:53