我需要你帮我完成一个项目。
我有三个变量,一个是日,另一个是月,最后一个是年。就像这样:
我有三个变量,一个是日,另一个是月,最后一个是年。就像这样:
var year = 2017
var month = 06
var day = 19
即使应用程序关闭,当我们到达这些变量的日期时,我想发送通知,但我对日历和日期并不是很了解。目前我只是制作了这个应用程序。let myNotification = Notification.Name(rawValue:"MyNotification")
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
nc.addObserver(forName:myNotification, object:nil, queue:nil, using:catchNotification)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let nc = NotificationCenter.default
nc.post(name:myNotification,
object: nil,
userInfo:["message":"Hello there!", "date":Date()])
}
func catchNotification(notification:Notification) -> Void {
print("Catch notification")
guard let userInfo = notification.userInfo,
let message = userInfo["message"] as? String,
let date = userInfo["date"] as? Date else {
print("No userInfo found in notification")
return
}
let alert = UIAlertController(title: "Notification!",
message:"\(message) received at \(date)",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
Thank you in advance
Date
对象,您可以使用年、月和日变量进行初始化。 - Dávid Pásztorlet yourFireDate = year + month + day
。 - Rombond