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.
UserDefaults
that areproperty list
types. In order to save a custom class/object, you need to convert it toNSData
by archiving it. See this answer – Invertebrate