Saving variables and booleans in swift 3 [closed]

若如初见. 提交于 2019-12-13 11:19:36

问题


I'm new to iOS development, currently in the process of creating my first app which its core function is a timer

I have a few different variables (for example start time of timer) and a boolean of whether the timer is running or not.

Is there a way where I can save the timer start time or boolean values even when the app is closed?

I'm using NSDate to store the start time and comparing the current time to this in order to produce a value of time passed.

Thanks in advance!


回答1:


Swift 3.x

Set and get the Date object

UserDefaults.standard.set(Date(), forKey: "lastDate")
UserDefaults.standard.object(forKey: "lastDate") as? Date

Set and get the Bool object

UserDefaults.standard.set(true, forKey: "isOn")
UserDefaults.standard.bool(forKey: "isOn")


来源:https://stackoverflow.com/questions/45351787/saving-variables-and-booleans-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!