我正在尝试在用户关闭应用后24小时发送通知。我在Objective-C中完成了以下操作,但是现在,我正在努力学习快速并需要一些帮助
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
notification.alertBody = @"24 hours passed since last visit :(";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
上面的代码就是我在Objective-C中使用的代码。
我在Swift中尝试了以下内容但收到了错误消息
var notification:UILocalNotification = UILocalNotification()
notification.alertBody = "Hi, I am a notification"
notification.fireDate = NSDate.dateByAddingTimeInterval(60*60*24);
错误消息是“'(NSTimeInterval) - > NSDate!'不能转换为NSDate“
我希望得到一些帮助,看看我做错了什么,因为对我来说,它看起来很好。但是,我不是一个经验丰富的程序员。
答案 0 :(得分:2)
NSTimeInterval是双精度数,因此您必须将其转换。
var notification: UILocalNotification = UILocalNotification()
notification.alertBody = "Hi, I am a notification"
let myDate: NSDate = NSDate()
notification.fireDate = myDate.dateByAddingTimeInterval(Double(60*60*24));