Ios 从类型为';的抛出函数进行的转换无效;()抛出->;无效';到非抛出函数类型';([未通知请求])->;无效的

Ios 从类型为';的抛出函数进行的转换无效;()抛出->;无效';到非抛出函数类型';([未通知请求])->;无效的,ios,swift,uilocalnotification,unusernotificationcenter,Ios,Swift,Uilocalnotification,Unusernotificationcenter,我正在尝试获取本地通知的挂起通知请求。 这给我带来了一个错误: “从类型为“(\u)throws->Void”的抛出函数到非抛出函数类型“([UNNotificationRequest])->Void”的转换无效” 我的代码是: var notificationTitle = "\(String(describing: notificationData!["title"]))" var notificationInterval: Int = notificationData!["interval

我正在尝试获取本地通知的挂起通知请求。 这给我带来了一个错误: “从类型为“(\u)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)
}

我不确定您的代码的哪一部分被抛出,但这一行代码不是真的:

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

你可能想这样做

    center.getPendingNotificationRequests(completionHandler: {requests -> () in
        var notificationExist:Bool = false
        for notificationRequest in requests {
            do {
                var notificationContent:UNNotificationContent = try notificationRequest.content
            }
            catch {
                print(error)
            }
        }
    }

问题出在您的try块中。因此,您可以按如下所示替换它

guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
    print("There was an error!")
}

问题出在您的try块中。因此,您可以按如下所示替换它

guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
    print("There was an error!")
}