我在这里很困惑 - 我正在尝试检测我的应用程序是否从 LocalNotification 启动。但我所有的代码都被破坏了。
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
var firstWay = launchOptions.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey)
var secondWay = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]
return true
}
这两个都失败并显示消息
"unexpectedly found nil while unwrapping an Optional value"
我确信我在这里做错了一些非常基本的事情。有什么指点吗?
您正在展开参数中的 launchOptions 字典,该字典通常为 nil。尝试解开 nil 值会导致崩溃,因此您需要在使用尾随感叹号解开它之前检查它是否不是 nil。正确的代码如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
if let options = launchOptions {
// Do your checking on options here
}
return true
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)