Xcode 通过在Swift中添加时间间隔发送带有日期的本地推送通知
我试图在用户关闭应用程序24小时后发送通知。我已经在Objective-C中完成了以下工作,但现在,我正在尝试学习swift,需要一些帮助Xcode 通过在Swift中添加时间间隔发送带有日期的本地推送通知,xcode,swift,uilocalnotification,Xcode,Swift,Uilocalnotification,我试图在用户关闭应用程序24小时后发送通知。我已经在Objective-C中完成了以下工作,但现在,我正在尝试学习swift,需要一些帮助 UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24]; notification.alertBody = @"24 ho
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”
我希望能得到一些帮助,看看我做错了什么,因为对我来说,这看起来很好。但是,我不是一个经验丰富的程序员。NSTimeInterval是双精度的,所以您必须转换它
var notification: UILocalNotification = UILocalNotification()
notification.alertBody = "Hi, I am a notification"
let myDate: NSDate = NSDate()
notification.fireDate = myDate.dateByAddingTimeInterval(Double(60*60*24));
短一点:notification.fireDate=NSDate(timeIntervalSinceNow:Double(60*60*24))