Я пытаюсь получить ожидающий запрос уведомления о локальном уведомлении. Это вызывает у меня ошибку: "Недопустимое преобразование из функции бросания типа '(_) throws -> Void' в функцию без метания '([UNNotificationRequest]) -> Void'»
Мой код:
var notificationTitle = "\(String(describing: notificationData!["title"]))"
var notificationInterval: Int = notificationData!["interval"] as! Int
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: {(requests) -> Void in
var notificationExist:Bool = false
for notificationRequest in requests {
try{
var notificationContent:UNNotificationContent = notificationRequest.content
}
}
Я не уверен, какая часть вашего кода выбрасывается, но эта строка вашего кода неверна:
try{
var notificationContent:UNNotificationContent = notificationRequest.content
}
Правильный способ:
do {
var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
Вы можете сделать это так,
center.getPendingNotificationRequests(completionHandler: {requests -> () in
var notificationExist:Bool = false
for notificationRequest in requests {
do {
var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
}
}
Проблема в вашем блоке попытки. Итак, вы можете заменить его, как показано ниже.
guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
print("There was an error!")
}