在 Swift 3 中检查 launchOptions [英] Checking launchOptions in Swift 3
本文介绍了在 Swift 3 中检查 launchOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将代码转换为 swift 3,并已提交至应用商店.当他们打开应用程序时,它第一次崩溃.结果,我检查了我的崩溃日志,它在这一行崩溃了.
I have converted my codes to swift 3 and I have submitted to app store. When they open app, it crash at first time. As a result, I check my crashlog and it crash at this line.
if let myLaunchOptions: NSDictionary = launchOptions as NSDictionary? {
我的整体代码是这样的.我知道 launchOptions 可以为零,甚至可能不是 NSDictionary.这就是为什么我像这样检查并且在那条线上失败了.我可以知道如何使用 swift 3 检查/预防吗?
My overall code is like this. I know that launchOptions can be nil and it might not even be NSDictionary. That's why I have checked like that and it fail at that line. May I know how else to check/prevent with swift 3?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let myLaunchOptions: NSDictionary = launchOptions as NSDictionary? {
let test = myLaunchOptions[UIApplicationLaunchOptionsKey.userActivityDictionary] as! NSDictionary
let userActivity = test["UIApplicationLaunchOptionsUserActivityKey"] as! NSUserActivity
NSLog("test1:" + String(describing: userActivity))
continueUserActivity(userActivity)
}
我的崩溃日志在这里.
推荐答案
您应该像这样检查和获取用户活动:
You should be checking and obtaining user activity like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let userActivityDictionary = launchOptions?[.userActivityDictionary] as? [UIApplicationLaunchOptionsKey : Any],
let userActivity = userActivityDictionary[.userActivityType] as? NSUserActivity {
continueUserActivity(userActivity)
}
return true
}
这篇关于在 Swift 3 中检查 launchOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文