Search code examples
iosswiftuilocalnotificationunusernotificationcenter

Invalid conversion from throwing function of type '(_) throws -> Void' to non-throwing function type '([UNNotificationRequest]) -> Void


I am trying to get pending notification request on local notification. It throws me error: "Invalid conversion from throwing function of type '(_) throws -> Void' to non-throwing function type '([UNNotificationRequest]) -> Void' "

My code is:

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
        }
    }

Solution

  • I'm not sure which part of your code is throwing but this line of your code is not true:

    try{
        var notificationContent:UNNotificationContent = notificationRequest.content
        }
    

    The correct way of it is this:

    do {
        var notificationContent:UNNotificationContent = try notificationRequest.content
    }
    catch {
    print(error)
    }