繁体   English   中英

从类型为((_)的throwing函数-> Void'无效转换为类型为非抛出函数'([[UNNotificationRequest])-> Void

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

我正在尝试获取有关本地通知的待处理通知请求。 它引发了我错误: “从类型为((_)的throwing函数将-> 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
        }
    }

您可能想要这样,

    center.getPendingNotificationRequests(completionHandler: {requests -> () in
        var notificationExist:Bool = false
        for notificationRequest in requests {
            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)
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM