我需要您的帮助来完成一个项目。
我有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