首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从“(_)抛出->空”类型的抛出函数到非抛出函数类型的无效转换([UNNotificationRequest]) ->空

从“(_)抛出->空”类型的抛出函数到非抛出函数类型的无效转换([UNNotificationRequest]) ->空
EN

Stack Overflow用户
提问于 2018-05-02 06:54:22
回答 3查看 180关注 0票数 0

我正在尝试在本地通知时获得挂起的通知请求。它抛出错误:“从类型的抛出函数'(_)到非抛函数类型的无效转换”(UNNotificationRequest) -> Void‘"

我的代码是:

代码语言:javascript
运行
复制
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
        }
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-02 07:03:59

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

代码语言:javascript
运行
复制
try{
    var notificationContent:UNNotificationContent = notificationRequest.content
    }

正确的做法是:

代码语言:javascript
运行
复制
do {
    var notificationContent:UNNotificationContent = try notificationRequest.content
}
catch {
print(error)
}
票数 1
EN

Stack Overflow用户

发布于 2018-05-02 07:11:09

你也许想这样做,

代码语言:javascript
运行
复制
    center.getPendingNotificationRequests(completionHandler: {requests -> () in
        var notificationExist:Bool = false
        for notificationRequest in requests {
            do {
                var notificationContent:UNNotificationContent = try notificationRequest.content
            }
            catch {
                print(error)
            }
        }
    }
票数 3
EN

Stack Overflow用户

发布于 2018-05-02 07:14:36

问题在于你的尝试块。因此,您可以替换它,如下所示。

代码语言:javascript
运行
复制
guard let notificationContent:UNNotificationContent = try? notificationRequest.content else {
    print("There was an error!")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50128723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档