在特定时间Swift设置通知

时间:2015-12-24 03:11:50

标签: ios swift

我想在我设置的时间(小时,分钟)设置通知。但它显示错误:

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)
  }

1 个答案:

答案 0 :(得分:1)

正如保罗在评论中指出的那样,错误信息告诉你错误。

您需要将通知的fireDate属性设置为日期(An NSDate)。您需要一个将日期组件转换为NSDate的方法。 NSCalendar方法dateFromComponents:怎么样?