ios - Using NSUserDefaults on arrays -


i trying use nsuserdefaults save array in app's core data. thought use nsuserdefaults problem wherever put code creates default throws sigabrt error.

here code creates default:

let levelarraydefault = nsuserdefaults.standarduserdefaults()     levelarraydefault.setvalue(levelsarray, forkey: "levelsarray")     levelarraydefault.synchronize() 

levelsarray array of list objects:

    class list: nsobject, nscoding {     // mark: properties     var name: string     var answersarray = [answer]()       init?(name: string) {         // initialize stored properties.         self.name = name          if name.isempty {             return nil         }      }      required init(coder decoder: nscoder){         self.answersarray = (decoder.decodeobjectforkey("aa") as? [answer])!         self.name = (decoder.decodeobjectforkey("name") as? string)!     }     func encodewithcoder(coder: nscoder) {         if let answersarray = answersarray { coder.encodeobject(answersarray, forkey: "aa") }         if let name = name { coder.encodeobject(name, forkey: "name") }     }   } class answer: nsobject, nscoding {     var englishanswer: string = ""     var chineseanswer: string = ""     init(neweng: string, newchi: string){         self.englishanswer = neweng         self.chineseanswer = newchi     }     required init(coder decoder: nscoder){         self.englishanswer = (decoder.decodeobjectforkey("englishanswer") as? string)!         self.chineseanswer = (decoder.decodeobjectforkey("chineseanswer") as? string)!     }     func encodewithcoder(coder: nscoder) {         if let englishanswer = englishanswer { coder.encodeobject(englishanswer, forkey: "englishanswer") }         if let chineseanswer = chineseanswer { coder.encodeobject(chineseanswer, forkey: "chineseanswer") }     }  } 

how can stop sigabrt popping , array stored. appreciated.

you need convert nsdata using nskeyedarchiver before storing nsuserdefaults, try this:

update: xcode 8.2.1 • swift 3.0.2

import uikit class viewcontroller: uiviewcontroller {     override func viewdidload() {         super.viewdidload()         let list = list(name: "student")         list.answers = [answer(english: "english answer", chinese: "中文回答")]         let data = nskeyedarchiver.archiveddata(withrootobject: [list])         userdefaults.standard.set(data, forkey: "listdata")         guard             let loadeddata = userdefaults.standard.data(forkey: "listdata"),             let loadedarray = nskeyedunarchiver.unarchiveobject(with: loadeddata) as? [list]             else { return }         print(loadeddata.count)         print(loadedarray.first ?? "none")         print(loadedarray.first?.name ?? "no name")         print(loadedarray.first?.answers.first?.english ?? "no english")         print(loadedarray.first?.answers.first?.chinese ?? "no chinese")     } } 

class answer: nsobject, nscoding {     let english: string     let chinese: string     init(english: string, chinese: string) {         self.english = english         self.chinese = chinese     }     required init(coder decoder: nscoder) {         self.english = decoder.decodestring(forkey: "english")         self.chinese = decoder.decodestring(forkey: "chinese")     }     func encode(with coder: nscoder) {         coder.encode(english, forkey: "english")         coder.encode(chinese, forkey: "chinese")     } } 

class list: nsobject, nscoding {     let name: string     fileprivate var data = data()     var answers: [answer] {         {             return nskeyedunarchiver.unarchiveobject(with: data) as? [answer] ?? []         }         set {             data = nskeyedarchiver.archiveddata(withrootobject: newvalue)         }     }     init(name: string) {         self.name = name     }     required init(coder decoder: nscoder) {         self.data = decoder.decodedata(forkey: "answersdata")         self.name = decoder.decodestring(forkey: "name")     }     func encode(with coder: nscoder) {         coder.encode(data, forkey: "answersdata")         coder.encode(name, forkey: "name")     } } 

extension nscoder {     func decodestring(forkey key: string) -> string {         return decodeobject(forkey: key) as? string ?? ""     }     func decodedata(forkey key: string) -> data {         return decodeobject(forkey: key) as? data ?? data()     } } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -