【问题标题】:Convert NSCalendarUnit to Swift2 [duplicate]将 NSCalendarUnit 转换为 Swift2 [重复]
【发布时间】:2016-01-20 19:25:51
【问题描述】:

我有下一个代码:

var notification = UILocalNotification()
    notification.alertBody = "Message" // text that will be displayed in the notification
    notification.fireDate = NSDate()  // right now (when notification will be fired)
    notification.soundName = UILocalNotificationDefaultSoundName // play default sound

    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Minute], fromDate: NSDate())

    notification.repeatInterval = NSCalendarUnit.CalendarUnitHour // this line defines the interval at which the notification will be repeated
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

但它已被弃用。搜索了很多,但无法将其转换为 Swift2。谁能帮我解决这个问题:

notification.repeatInterval = NSCalendarUnit.CalendarUnitHour

【问题讨论】:

  • @LeoDabus 我做了,但它不起作用。它不发送通知 =/
  • 检查一下,您注册了通知吗? stackoverflow.com/a/34575836/2303865
  • @LeoDabus 是的。在第一次启动时,它会询问我有关通知的信息。我已将以上所有行放入 showNotification() 函数并在 viewWillDisappear 上调用它。但它甚至根本不调用

标签: ios swift uilocalnotification nscalendar


【解决方案1】:

Swift 2 中的枚举比 Objective-C 中的要好得多。

尝试下一行:

notification.repeatInterval = NSCalendarUnit.Hour

它应该可以工作。

【讨论】:

  • 但它根本不起作用=/
  • 您遇到了哪个错误?这意味着问题不在这一行。
  • 我已将以上所有行放入showNotification() func 并在viewWillDisappear 上调用它。但它甚至根本不调用
  • 那么,为什么你确定问题出在这行代码?
  • 不,是的。不过谢谢你的回答我解决了。但它仍然不起作用,这就是我问你的原因。如果你能帮助我,那就太好了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 2015-08-06
相关资源
最近更新 更多