如果我的应用程序在整个运行时都使用它,它应该如何处理当前日期?
How should my app handle the current date if it uses it throughout its runtime?
我应该在哪里放置 Date 实例以及我应该如何处理调用它以获得当前日期?我应该用日期实例创建一个单例吗?我是否应该在 AppDelegate 的功能之一中调用此实例,以便在应用程序未使用时更新当前日期?
"Where should I put a Date instance and how should I handle calling it in order to get the current date? "
在您当前的 View Controller 中就足够了。只要 VC 还活着,它
对您的 Date 对象有很强的引用。
"Should I make a singleton with a date instance?"
不。如果你需要保持日期相关,你需要触发一个计时器(比如 1 分钟
所以它不会打扰太多)这将使 UI
中的日期更新
"Should I call this instance in one of AppDelegate's functions in order to update the current date when the app isn't on use?"
不,您可以获得通知,并通过它们知道何时启动或关闭计时器。
见代码
class ViewController: UIViewController{
// label to hold the date
@IBOutlet var dateLabel: UILabel!
// timer to keep it updated
var fetchTimer: Timer!
override func viewDidLoad()
{
super.viewDidLoad()
// set date immediately (dont wait for timer)
viewDidEnterForeground()
// follow Foreground so when we re-enter, timer will launch again
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.viewDidEnterForeground),
name:NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
// follow background for invalidating timer
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.viewDidEnterBackground),
name:NSNotification.Name.UIApplicationDidEnterBackground,
object: nil)
}
// on each entry - set date and fire timer
func viewDidEnterForeground()
{
setDate()
fetchTimer = Timer.scheduledTimer(timeInterval: 60.0,
target: self,
selector: #selector(timerFunc),
userInfo: nil,
repeats: true)
}
func viewDidEnterBackground()
{
fetchTimer.invalidate()
}
func timerFunc()
{
setDate()
}
func setDate()
{
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
// "22.04.2017"
let dateFormatString = formatter.string(from: date)
DispatchQueue.main.async
{
self.dateLabel.text = dateFormatString
}
}
deinit
{
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
}
你的问题部分没有真正意义:
"Where should I put a Date instance and how should I handle calling it in order to get the current date?"
Date 实例记录了一个固定的时间点。代码
let date = Date()
会在调用的那一刻记录当前日期,不会改变。如果您的计划明天仍然是 运行,那么该日期现在将非常 "out of date"。
相反,您应该在任何需要当前日期的时候使用表达式 Date()
。
我应该在哪里放置 Date 实例以及我应该如何处理调用它以获得当前日期?我应该用日期实例创建一个单例吗?我是否应该在 AppDelegate 的功能之一中调用此实例,以便在应用程序未使用时更新当前日期?
"Where should I put a Date instance and how should I handle calling it in order to get the current date? " 在您当前的 View Controller 中就足够了。只要 VC 还活着,它 对您的 Date 对象有很强的引用。
"Should I make a singleton with a date instance?" 不。如果你需要保持日期相关,你需要触发一个计时器(比如 1 分钟 所以它不会打扰太多)这将使 UI
中的日期更新"Should I call this instance in one of AppDelegate's functions in order to update the current date when the app isn't on use?" 不,您可以获得通知,并通过它们知道何时启动或关闭计时器。 见代码
class ViewController: UIViewController{
// label to hold the date
@IBOutlet var dateLabel: UILabel!
// timer to keep it updated
var fetchTimer: Timer!
override func viewDidLoad()
{
super.viewDidLoad()
// set date immediately (dont wait for timer)
viewDidEnterForeground()
// follow Foreground so when we re-enter, timer will launch again
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.viewDidEnterForeground),
name:NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
// follow background for invalidating timer
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.viewDidEnterBackground),
name:NSNotification.Name.UIApplicationDidEnterBackground,
object: nil)
}
// on each entry - set date and fire timer
func viewDidEnterForeground()
{
setDate()
fetchTimer = Timer.scheduledTimer(timeInterval: 60.0,
target: self,
selector: #selector(timerFunc),
userInfo: nil,
repeats: true)
}
func viewDidEnterBackground()
{
fetchTimer.invalidate()
}
func timerFunc()
{
setDate()
}
func setDate()
{
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
// "22.04.2017"
let dateFormatString = formatter.string(from: date)
DispatchQueue.main.async
{
self.dateLabel.text = dateFormatString
}
}
deinit
{
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
}
你的问题部分没有真正意义:
"Where should I put a Date instance and how should I handle calling it in order to get the current date?"
Date 实例记录了一个固定的时间点。代码
let date = Date()
会在调用的那一刻记录当前日期,不会改变。如果您的计划明天仍然是 运行,那么该日期现在将非常 "out of date"。
相反,您应该在任何需要当前日期的时候使用表达式 Date()
。