Ian Thompson
Ian Thompson

Reputation: 51

Sending a Local Push Notification with dateByAddingTimeInterval in Swift

I am trying to send a notification about 24 hours after the user has closed the app. I have done the following in Objective-C, but now, am trying to learn swift and need a little help

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];

The code above is what I used in Objective-C.

I tried the following in Swift but got an error message

 var notification:UILocalNotification = UILocalNotification()
    notification.alertBody = "Hi, I am a notification"
    notification.fireDate = NSDate.dateByAddingTimeInterval(60*60*24);

The error Message is "'(NSTimeInterval) -> NSDate!' is not convertible to NSDate"

I would love some help to see what I've done wrong, because to me, it looks fine. But, I am no seasoned programmer.

Upvotes: 2

Views: 755

Answers (1)

mstottrop
mstottrop

Reputation: 589

A NSTimeInterval is a double, so you will have to convert it.

var notification: UILocalNotification = UILocalNotification()
notification.alertBody = "Hi, I am a notification"
let myDate: NSDate = NSDate()
notification.fireDate = myDate.dateByAddingTimeInterval(Double(60*60*24));

Upvotes: 2

Related Questions