我正在尝试通过本地通知获取待处理通知请求。 它引发了我的错误: "类型'(_)抛出函数的转换无效 - >虚空'到非投掷功能类型'([UNNotificationRequest]) - >虚空' "
我的代码是:
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
}
}
答案 0 :(得分:3)
你可能想这样做,
center.getPendingNotificationRequests(completionHandler: {requests -> () in
var notificationExist:Bool = false
for notificationRequest in requests {
do {
var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
}
}
答案 1 :(得分:1)
我不确定您的代码的哪一部分正在抛出,但是这行代码并不正确:
try{
var notificationContent:UNNotificationContent = notificationRequest.content
}
正确的方法是:
do {
var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
答案 2 :(得分:0)
问题出在你的try块中。因此,您可以替换它,如下所示。
guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
print("There was an error!")
}