ios - Swift 3 Date type not encoding as part of custom class -


i'm having difficulty encoding , decoding date type property of custom swift class nskeyedarchiver. call keyedarchiver with:

let saveddata = nskeyedarchiver.archiveddata(withrootobject: timers)     let defaults = userdefaults.standard     defaults.set(saveddata, forkey: "timers") 

this top half of class declaration:

class timermodel: nsobject, nscoding, avaudioplayerdelegate {  var name: string var active: bool var paused: bool var duration: int var remainingwhenpaused: int? var timerendtime: date? var timerstarttime: date? var audioalert: alertnoise var uuid: string var colorscheme: basecolor var alarmrepetitions: int var timerrepetitions: int var currenttimerrepetition: int var audioplaying: bool var player: avaudioplayer = avaudioplayer() var countdowntimer: timer = timer() var delegate: timerprotocol? = nil  init(withname name: string, duration: int, uuid: string, color: basecolor, alertnoise: alertnoise, timerrepetitions: int, alarmrepetitions: int, timerendtime: date? = nil) {     self.name = name     self.active = false     self.paused = false     self.duration = duration     self.uuid = uuid     self.audioalert = alertnoise     self.colorscheme = color     self.alarmrepetitions = alarmrepetitions     self.audioplaying = false     self.timerrepetitions = timerrepetitions     self.currenttimerrepetition = 0      super.init() }  convenience override init() {     self.init(withname: "tap timer 1", duration: 10, uuid: foundation.uuid().uuidstring, color: .red, alertnoise: .churchbell, timerrepetitions: 1, alarmrepetitions: 0) }  // mark: nscoding  required convenience init? (coder decoder: nscoder) {      guard let name = decoder.decodeobject(forkey: "name") as? string     else {         print("init coder name guard failed")         return nil     }      let duration = decoder.decodeinteger(forkey: "duration")      guard let audioalertrawvalue = decoder.decodeobject(forkey: "audioalert") as? string     else {         print("init coder audioalert guard failed")         return nil     }     guard let uuid = decoder.decodeobject(forkey: "uuid") as? string     else {         print("init coder uuid guard failed")         return nil     }     guard let colorschemerawvalue = decoder.decodeobject(forkey: "colorscheme") as? string     else {         print("init coder colorscheme guard failed")         return nil     }      let alarmrepetitions = decoder.decodeinteger(forkey: "alarmrepetitions")      let timerrepetitions = decoder.decodeinteger(forkey: "timerrepetitions")      guard let audioalert = alertnoise(rawvalue: audioalertrawvalue)         else{             print("no alertnoise rawvalue case found")             return nil     }     guard let colorscheme = basecolor(rawvalue: colorschemerawvalue)         else{             print("no basecolor rawvalue case found")             return nil     }      var timerendtimeunwrapped: date?      if let timerendtime = decoder.decodeobject(forkey: "timerendtime") as? date {         timerendtimeunwrapped = timerendtime     } else {         timerendtimeunwrapped = nil     }     print("initcoder guards passed, initing timer")     print("\(name), \(duration), \(uuid), \(colorscheme), \(audioalert), \(timerrepetitions), \(alarmrepetitions), \(timerendtimeunwrapped)")      self.init(withname: name, duration: duration, uuid: uuid, color: colorscheme, alertnoise: audioalert, timerrepetitions: timerrepetitions, alarmrepetitions: alarmrepetitions, timerendtime: timerendtimeunwrapped) }  func encode(with acoder: nscoder) {      acoder.encode(self.name, forkey: "name")     acoder.encode(self.duration, forkey: "duration")     acoder.encode(self.audioalert.rawvalue, forkey: "audioalert")     acoder.encode(self.uuid, forkey: "uuid")     acoder.encode(self.colorscheme.rawvalue, forkey: "colorscheme")     acoder.encode(self.alarmrepetitions, forkey: "alarmrepetitions")     acoder.encode(self.timerrepetitions, forkey: "timerrepetitions")     acoder.encode(self.timerendtime, forkey: "timerendtime") } 

when try unarchive object nsuserdefaults timerendtimeunwrapped comes nil. either i've messed unwrapping thing or either encode or decode failing. else comes expected, it's date type problematic

any great.

so stupid mistake, problem adding properties once class being used lot. wasn't setting timerendtime in init function of class!


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -