我需要您的幫助來完成一個項目。
我有3個變量,一個爲當天,另一個爲本月和最後一年。這樣的:Swift 3 - 特定日期的本地通知
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)
}
預先感謝您
「yourFireDate」的值是什麼? – Rombond
它是一個'Date'對象,它使用變量year,month和day來初始化。 –
所以我設置了'let yourFireDate = year + month + day' – Rombond