【问题标题】:Initializer for conditional binding must have Optional type, not 'String'?条件绑定的初始化程序必须具有可选类型,而不是“字符串”?
【发布时间】:2018-11-06 21:19:05
【问题描述】:

无法读取 FCM 通知用户数据,我正在尝试这个 -

    let userInfo = response.notification.request.content.userInfo

             if let jsonResult = userInfo as? Dictionary<String, AnyObject> //**Getting error on this line**
{

             if let notifyType = 
              jsonResult["gcm.notification.notification_type"] as? String ?? "" 
              {

                print(notifyType)           

              }
            }

【问题讨论】:

  • 错误出现在哪一行?
  • @iDeveloper 您还必须显示您的 JSON 响应。请编辑您的问题
  • 一些旁注:如果让 X 表示条件绑定,如果右侧是可选的。既然你用了……?? "",表示当值为 nil 时,您已经提供了默认值,这意味着它不是可选值。如果 let X 因此无效。
  • @J.Doe 感谢您的深入描述。

标签: swift firebase push-notification firebase-cloud-messaging


【解决方案1】:

在您的代码中 替换:

  if let notifyType = 
      jsonResult["gcm.notification.notification_type"] as? String ?? "" 
      {

与:

  if let notifyType = 
      jsonResult["gcm.notification.notification_type"] as? String {

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 2016-01-29
    • 1970-01-01
    • 2018-12-20
    • 2019-03-11
    • 2017-01-22
    • 2018-03-25
    • 2016-01-08
    • 2017-04-08
    相关资源
    最近更新 更多