我正在尝试在本地通知时获得挂起的通知请求。它抛出错误:“从类型的抛出函数'(_)到非抛函数类型的无效转换”(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
}
}发布于 2018-05-02 07:03:59
我不确定您的代码的哪一部分正在抛出,但是代码的这一行不正确:
try{
var notificationContent:UNNotificationContent = notificationRequest.content
}正确的做法是:
do {
var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}发布于 2018-05-02 07:11:09
你也许想这样做,
center.getPendingNotificationRequests(completionHandler: {requests -> () in
var notificationExist:Bool = false
for notificationRequest in requests {
do {
var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
}
}发布于 2018-05-02 07:14:36
问题在于你的尝试块。因此,您可以替换它,如下所示。
guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
print("There was an error!")
}https://stackoverflow.com/questions/50128723
复制相似问题