saving swifty json array to user defaults

 ̄綄美尐妖づ 提交于 2021-02-16 16:36:23

问题


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 }
    ]
  }
]

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

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

However, I get an error

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

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


回答1:


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.

Just call arrayObject on the JSON object:

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



回答2:


Swift 3.0

You can save SwiftyJSON custom type data as like below.

guard let rawData = try? json.rawData() else { return }
UserDefaults.standard.setValue(rawData, forKey: "Your_key")

And, To retrieve JSON data from UserDefaults, you can do like this way.

guard let data = UserDefaults.standard.value(forKey: "Your_key") as? Data else { return }

let json = JSON(data) 



回答3:


You can't save the array of dictionary directly to the NSUserDefault, You need to convert the object of array into NSData

See more for help:

Attempt to set a non-property-list object as an NSUserDefaults




回答4:


Swift 4.0 | Xcode 9.1 | SwiftyJSON

We can save array of dictionary directly, no problem. Only, the problem is, how to retrieve it?

It barely downcast to array of dictionary (say [[String : Any]]). for this, we required SwiftyJSON pods import SwiftyJSON // Don't forget this

let arrayOfDictionary = [
                         { 
                           "VersionNumber" : "1.0.0.0", 
                                  "FormID" : "18"
                          },
                         {
                           "VersionNumber" : "1.0.0.3",
                                  "FormID" : "19"
                          }
                         ]

Saving it directly to UserDefaults like :

UserDefaults.standard.set(saveFormDetails, forKey: "FormDetails")

Retrieving from UserDefaults :

let savedFormDetailsData = JSON.init(rawValue:UserDefaults.standard.array(forKey: "FormDetails"))

Now, savedFormDetailsData is the array of dictionary back [PropertyList Type], for further we do :

for form in 0..<arrayOfDictionary.count {
    print("FormID :", savedFormDetailsData?[form]["FormID"]) 
    print("VersionNumber :", savedFormDetailsData?[form]["VersionNumber"])
}


来源:https://stackoverflow.com/questions/45457523/saving-swifty-json-array-to-user-defaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!