ios - How do I save a custom class in swift to NSUserDefaults? -


this question has answer here:

i have made custom class called test. made variable called "game" equal test. tried save "game" nsuserdefaults , attempted print values "game". after running project got error: "thread 1:signal sigabrt". how save custom object nsuserdefaults without getting error?

here class:

class test {     var boolean = false     var int = 1     var arr = ["one": 1] } 

here saving code:

    let defaults = nsuserdefaults.standarduserdefaults()      let game = test()      game.arr = ["two": 3]     game.hi = true     game.int = 100      defaults.setobject(game, forkey: "sg")      let gameset = defaults.objectforkey("sg")! as! test      print(gameset.int)     print(gameset.boolean)     print(gameset.arr["one"]) 

this in viewdidload method. excluding test class.

from documentation:

the value parameter can property list objects: nsdata, nsstring, nsnumber, nsdate, nsarray, or nsdictionary.

you'll want @ using nskeyedarchiver instead custom objects.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -