ios - swift 的ios。 fatal error : Unexpectedly found nil while unwrapping an Optional value

标签 ios swift

<分区>

如何解决上述问题。谢谢。

let notificationTypes : UIUserNotificationType = [UIUserNotificationType.alert , UIUserNotificationType.badge, UIUserNotificationType.sound]
let notificationSettings = UIUserNotificationSettings(types: notificationTypes,categories : nil)
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)

return true

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

print("MessageID: \(userInfo["gcm_message_id"])") //the error is in this line
print(userInfo)

最佳答案

错误指出您正在尝试访问一个 nil 值。您应该有条件地解包该值,这样它就不会尝试访问 nil 值。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

  if let message = userInfo["gcm_message_id"] {
    print("MessageID: \(message)")
  }
  print(userInfo)
}

关于ios - swift 的ios。 fatal error : Unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49483291/

相关文章:

ios - 即使在弹出 View Controller 之后,CLLocationManager 委托(delegate)仍然被调用

swift - 在 View Controller 之间传递数据使用从导航 Controller 中嵌入的 View 到 tabbarcontroller 的 segue

ios - SKLabelNode 没有显示正确的 CGPoint

ios - 如何在核心数据中高效地插入和获取UUID

objective-c - 从 NSuserDefaults 取消归档数据的正确方法是什么?

iphone - 如何使用自定义 inputView 设计 iOS 程序

iphone - 以编程方式重置 UISearchbar

ios - 如何摆脱导航栏和手机顶部之间的空间

ios - 如何在 iOS 模拟器中禁用系统位置警报

swift - 我可以使用自定义 View 作为 NSSplitView 的分隔线吗?