Twitter khuong291
Twitter khuong291

Reputation: 11672

Set notification at specific time Swift

I want to set notification at the time (hour, minute) that I set. But It show error:

enter image description here

  func notification(story: Story) {
    let dateComponents = NSDateComponents.init()
    dateComponents.weekday = 5
    dateComponents.hour = story.remindeAtHour
    dateComponents.minute = story.remindeAtMinute
    let notification = UILocalNotification()
    notification.alertAction = "Title"
    notification.alertBody = "It's time to take a photo"
    notification.repeatInterval = NSCalendarUnit.WeekOfYear
    notification.fireDate = dateComponents.calendar
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
  }

Upvotes: 0

Views: 281

Answers (1)

Duncan C
Duncan C

Reputation: 131418

As Paul w points out in his comment, the error message is telling you what's wrong.

You need to set the notification's fireDate property to a date (An NSDate). You need a method that will convert date components to an NSDate. How about the NSCalendar method dateFromComponents:?

Upvotes: 1

Related Questions