ios - How to retrieve and assign a NSDate object from NSUserDefaults? -
I am creating a stopwatch application and I first lock the user application to store the start date and when it re Needing to get the user opens the application again.
For example, if the user starts the stopwatch and then closes the app and then reopens the application after a while, then the time between opening and closing the app should be added if The stopwatch was in progress, so the running time.
I have created two functions in your view controller that handles it's the code:
viewWillAppear function overrides (Animated: bool) do {startTimedefault: NSUserDefaults = NSUserDefaults KstandardUserDefaults () to startTimesaved: NSDate = startTimedefault.objectForKey ( "Start time") // this line bug if (Loncbul == true) {timer = NSTimer.scheduledTimerWithTimeInterval (1, target: self selector: "FireStopWatch", UserInfo: zero, repeats: true) startTime = startTimesaved} if (launchBool == incorrect) {timer .invalidate ()}} viewWillDisappear override function (animated: bu L) {NSUserDefaults.standardUserDefaults () setObject (launBool, forKey: "Start / Stop"). NSUserDefaults.standardUserDefaults () setObject (STARTTIME, forKey: "time start"). NSUserDefaults.standardUserDefaults () setObject (ElapsedTime, forKey: "time passed")}
I have gone through a couple of posts here on StackOverflow:
After
set your date object as follows:
UserDefaults. Standard.set (date (), forKey: "yourKey"): UserDefaults.standard.object (forKey:
reuse it "yourKey ") Like! Date
Comments
Post a Comment