ios 如何从 NSUserDefaults 检索和分配 NSDate 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26733628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve and assign a NSDate object from NSUserDefaults?
提问by Atharva Vaidya
I am building a stopwatch application and I need to store the start date before the user closes the application and need to retrieve it when the user opens the application again.
我正在构建一个秒表应用程序,我需要在用户关闭应用程序之前存储开始日期,并且需要在用户再次打开应用程序时检索它。
So for example if the user starts the stopwatch and then closes the application and then after some time opens the application again the app should add the time between the opening and closing to the running time if the stopwatch was running.
因此,例如,如果用户启动秒表,然后关闭应用程序,然后在一段时间后再次打开应用程序,如果秒表正在运行,应用程序应将打开和关闭之间的时间添加到运行时间中。
I have created two functions in my viewcontroller that handle this. Here's the code:
我在视图控制器中创建了两个函数来处理这个问题。这是代码:
override func viewWillAppear(animated: Bool)
{
let startTimedefault:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let startTimesaved:NSDate = startTimedefault.objectForKey("start time") // This line is buggy
if(launchBool == true)
{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "fireStopWatch", userInfo: nil, repeats: true)
startTime = startTimesaved
}
if(launchBool == false)
{
timer.invalidate()
}
}
override func viewWillDisappear(animated: Bool)
{
NSUserDefaults.standardUserDefaults().setObject(launchBool, forKey: "Start/Stop")
NSUserDefaults.standardUserDefaults().setObject(startTime, forKey: "start time")
NSUserDefaults.standardUserDefaults().setObject(elapsedTime, forKey: "elapsed time")
}
I have gone through a couple of posts here on StackOverflow :
我在 StackOverflow 上浏览了几篇文章:
What's the optimum way of storing an NSDate in NSUserDefaults?
在 NSUserDefaults 中存储 NSDate 的最佳方式是什么?
回答by Sujith Thankachan
Set your date object as follows:
设置您的日期对象如下:
UserDefaults.standard.set(Date(), forKey:"yourKey")
Retrieve it using:
使用以下方法检索它:
UserDefaults.standard.object(forKey: "yourKey") as? Date
Date will be optional of course, because it might never have been set.
日期当然是可选的,因为它可能永远不会被设置。
回答by KlimczakM
Swift 3/4/5version using Date
(type alias) instead of NSDate
:
Swift 3/4/5版本使用Date
(类型别名)代替NSDate
:
let yourDate = UserDefaults.standard.object(forKey: "yourKey") as? Date
UserDefaults.standard.set(yourDate, forKey: "yourKey")
Remember to use conditional cast operator as?
to avoid crashif there is no date yet under "yourKey" in UserDefaults
.
请记住,使用条件转换运算符as?
,以避免碰撞,如果没有时间未定“yourKey”下UserDefaults
。
回答by dengST30
In Swift 5,
在斯威夫特 5 中,
You can do some easy encapsulation for better managing the code
您可以做一些简单的封装以更好地管理代码
struct UserSetting {
static var shared = UserSetting()
var practiceTime: Date?{
get {
return UserDefaults.standard.object(forKey: TimeScaler.start) as? Date
}
set(newVal){
UserDefaults.standard.set(newVal, forKey: TimeScaler.start)
}
}
}
struct TimeScaler {
static let start = "start"
}