在 Swift 添加后接收 UILocalNotification

Receiving UILocalNotification after adding it in Swift

我在提问之前进行了搜索,但一无所获。我正在使用此代码在我的应用程序中安排通知:

    let firstFormatter = NSDateFormatter()
    firstFormatter.dateFormat = "MMMM, dd, HH:mm"

    let date = "\(dueDateTxtField.text) \(notificationTimeTxtField.text)" 

    let fireDate = firstFormatter.dateFromString(date)

    let realNotification = UILocalNotification()
    realNotification.fireDate = fireDate
    realNotification.soundName = UILocalNotificationDefaultSoundName
    realNotification.alertTitle = "Homework reminder"
    realNotification.alertBody = "You have a pending \(subjectTxtField.text) homework for tomorrow."


    let secondFormatter = NSDateFormatter()
    secondFormatter.dateFormat = "yyyy"

    let falseNotification = UILocalNotification()
    falseNotification.fireDate = secondFormatter.dateFromString("2020")
    falseNotification.soundName = UILocalNotificationDefaultSoundName
    falseNotification.alertTitle = "This notification will never get to you"
    falseNotification.alertBody = "Never"

    if notifyMeSegmentedControl.selectedSegmentIndex == 0 {
        UIApplication.sharedApplication().scheduleLocalNotification(realNotification)
    } else {
        UIApplication.sharedApplication().scheduleLocalNotification(falseNotification)
    }

我正在使用 UISegmentedControl,因此用户可以选择接收或不接收通知。事情是,只要我添加了一个新的家庭作业(该应用程序是一个家庭作业应用程序,您可以在其中添加主题、描述等),我已经收到通知,即使我将 "notificationTimeTxtField" 设置为时间不是现在。

请告诉我我做错了什么。 提前致谢!

编辑:我已尝试打印火灾日期,但打印为零!!我不知道为什么,文本字段中有文本!

我修好了。问题是 dueDateTxtField 的格式如下:"MMMM, dd, yyyy",而我只是尝试将其格式化为 "MMMM, dd"。所以我通过使用相同的格式格式化来修复它并且它完美地工作。