在swift 3中保存变量和布尔值 [英] Saving variables and booleans in swift 3

查看:46
本文介绍了在swift 3中保存变量和布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 开发新手,目前正在创建我的第一个应用程序,其核心功能是计时器

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?

我正在使用 NSDate 来存储开始时间并将当前时间与此时间进行比较,以生成经过的时间值.

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.

提前致谢!

推荐答案

Swift 3.x

设置和获取Date对象

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

设置和获取 Bool 对象

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

这篇关于在swift 3中保存变量和布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆