Ios 无法向我的报警应用程序执行通知

Ios 无法向我的报警应用程序执行通知,ios,swift,notifications,alarm,Ios,Swift,Notifications,Alarm,我创建了一个报警应用程序,它看起来与ios报警应用程序相似,单元格和tableview也与之相似。电池有两个标签和开关按钮,因此当我打开按钮时,警报在电池的标签时间被激活 有一个Triggeralum函数,它跟踪时间并循环通过类型为[Item]的items数组。Item是一个类型为NSManagedObject的类,用于保存数据。它有两个属性time:String&isOn:Bool 问题是viewDidLoad中的计时器方法没有通过Triggeralam func中的循环运行,而是实时打印。循

我创建了一个报警应用程序,它看起来与ios报警应用程序相似,单元格和tableview也与之相似。电池有两个标签和开关按钮,因此当我打开按钮时,警报在电池的标签时间被激活

有一个Triggeralum函数,它跟踪时间并循环通过类型为[Item]的items数组。Item是一个类型为NSManagedObject的类,用于保存数据。它有两个属性time:String&isOn:Bool

问题是viewDidLoad中的计时器方法没有通过Triggeralam func中的循环运行,而是实时打印。循环应该比较realTime with items数组,并在isOn属性为true但未发生时调用notificationTrigger func

Xcode也没有显示任何错误

代码如下:

import UIKit
import UserNotifications

class TableViewController: UITableViewController {

    var realTime  = String()
    var items     = [Item]()

    override func viewDidLoad() {
        super.viewDidLoad()
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})

    }

    override func viewWillAppear(_ animated: Bool) {

        getData()
        tableView.reloadData()

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell

        let row = items[indexPath.row]

        cell.timeLbl.text   = row.time
        cell.switchBtn.isOn = row.isOn

        cell.callback = { newValue in
            row.isOn = newValue
            (UIApplication.shared.delegate as! AppDelegate).saveContext()
        }
        return cell
    }

    func getData() {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        do {
             items = try context.fetch(Item.fetchRequest())
        }catch{
            print("\(error)")
        }
    }

    func triggerAlarm() {
        realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
        print(realTime)
        for i in 0..<items.count {
            if (items[i].time!) == realTime && items[i].isOn == true{
                print(time)
                self.notificationTrigger()
            }else {
                return
            }
        }
    }

    func notificationTrigger() {
        let content      = UNMutableNotificationContent()
        content.title    = "time is up \(time)"
        content.subtitle = "asdf"
        content.body     = "qwer"
        content.badge    = 0

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: "customNotification", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    }
}
在这里您使用的是else中的return。因此,假设您想要实现这一点,它不是中断当前iterartion的循环,而是从整个方法调用返回。而是使用:-

继续

或者最好省略其他部分

 @objc func triggerAlarm() {
    realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
    print(realTime)
    for i in 0..<items.count {
        if (items[i].time!) == realTime && items[i].isOn == true{
            print(time)
            self.notificationTrigger()
        }
    }
}

为什么在viewDidLoad和ViewWillDisplay中调用getData和tableView.reloadData?viewDidLoad中还有另一个问题。我可能已经复制并粘贴了,而不是剪切和粘贴了。谢谢,但是只有在我滚动tableview或从后台打开应用程序时才会显示通知
 @objc func triggerAlarm() {
    realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
    print(realTime)
    for i in 0..<items.count {
        if (items[i].time!) == realTime && items[i].isOn == true{
            print(time)
            self.notificationTrigger()
        }
    }
}