【问题标题】:Invalid conversion from throwing function of type '(_) throws -> Void' to non-throwing function type '([UNNotificationRequest]) -> Void从 '(_) throws -> Void' 类型的抛出函数到非抛出函数类型 '([UNNotificationRequest]) -> Void 的无效转换
【发布时间】:2018-05-02 06:54:22
【问题描述】:

我正在尝试获取有关本地通知的待处理通知请求。 它向我抛出错误: “从'(_) 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
        }
    }

【问题讨论】:

    标签: ios swift uilocalnotification unusernotificationcenter


    【解决方案1】:

    你可能想这样做,

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

    【讨论】:

      【解决方案2】:

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

      try{
          var notificationContent:UNNotificationContent = notificationRequest.content
          }
      

      正确的做法是这样的:

      do {
          var notificationContent:UNNotificationContent = try notificationRequest.content
      }
      catch {
      print(error)
      }
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2018-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-24
          • 2017-11-19
          • 1970-01-01
          相关资源
          最近更新 更多