Неверное преобразование из функции выброса типа '(_) throws -> Void' в тип функции не броска '([UNNotificationRequest]) -> Void - PullRequest
0 голосов
/ 02 мая 2018

Я пытаюсь получить ожидающий запрос уведомления о локальном уведомлении. Это выдает мне ошибку: "Неверное преобразование из функции выброса типа '(_) 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
        }
    }

Ответы [ 3 ]

0 голосов
/ 02 мая 2018

Возможно, вы захотите сделать это так,

    center.getPendingNotificationRequests(completionHandler: {requests -> () in
        var notificationExist:Bool = false
        for notificationRequest in requests {
            do {
                var notificationContent:UNNotificationContent = try notificationRequest.content
            }
            catch {
                print(error)
            }
        }
    }
0 голосов
/ 02 мая 2018

Проблема в вашем блоке try. Таким образом, вы можете заменить его, как показано ниже.

guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
    print("There was an error!")
}
0 голосов
/ 02 мая 2018

Я не уверен, какая часть вашего кода выдает, но эта строка вашего кода не соответствует действительности:

try{
    var notificationContent:UNNotificationContent = notificationRequest.content
    }

Правильный путь таков:

do {
    var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...