nsdate - Retrieve date from double value in NSUserDefaults (Swift) -
hi i'm creating has 2 controls: nsslider , nstextfield update continuously. added nsdateformatter nstextfield shows current value of slider formatted time. slider has range of 0 86,400 units (so user slides seconds in range of 24 hours).
i connected app shared user defaults controller value stored user.
the app shows correct time in nstextfield, stores value in seconds. example, if user selected 06:32:14 when moving slider, system store 23534.2261709793 in key starttime
when user opens app again, see 06:32:14 expected.
my problem when want read variable in code:
i can read value double:
let value = nsuserdefaults.standarduserdefaults().objectforkey("starttimevalue") as? double
this works fine, if try cast nsdate won't work (the value set nil). thought using nsdateformatter in code work (as way interface builder work user see date):
let dateformatter = nsdateformatter() dateformatter.datestyle = nsdateformatterstyle.nostyle dateformatter.timestyle = nsdateformatterstyle.shortstyle let date = dateformatter.datefromstring((value?.description)!)
in previous code created date formatter, set date style nothing (i want time), , time style short. giving value.description ("23534.2261709793") still won't work. date still nil.
is there i'm doing wrong?
the double storing / getting nsuserdefaults time interval thursday 1st january 1970. need feed time interval nsdate constructor can construct date you.
let value = nsuserdefaults.standarduserdefaults().objectforkey("starttimevalue") as? double let date = nsdate(timeintervalsince1970: value)
Comments
Post a Comment