Swift 3中没有UIApplicationLaunchOptionsShortcutItemKey这个东西?

7

最近在 Xcode 8 beta 6 (8S201h) 中出现了这个问题。

 UIApplicationLaunchOptionsShortcutItemKey

这是错误信息:

在此输入图片描述

还有其他人遇到了这个问题吗?

var performShortcutDelegate = true
if let shortcutItem = launchOptions[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
    print("ok")
    self.shortcutItem = shortcutItem
    performShortcutDelegate = false
}
return performShortcutDelegate

尝试使用 guard : https://dev59.com/KlsX5IYBdhLWcg3whv_Q - Bista
没有运气 :( 同样的错误 - Amit Kalra
你仍然收到“模糊的成员下标引用”错误吗?你的代码看起来是正确的,所以可能与封闭函数有关。还有可能需要在你的快捷方式代码周围包含块if #available(iOS 9.0, *) {}。更多信息/上下文将会很有帮助。 :) - Naftali Beder
所以我刚刚意识到你没有解包launchOptions,这意味着当你尝试使用它时,它仍然是Optional类型。你不能从可选字典中提取值,因为它在技术上不是一个字典。那很可能就是问题所在。我已经更新了我的答案来反映这一点,并包括了封闭函数。让我知道它是否有效! - Naftali Beder
这在 Xcode 8 GM 中仍然没有工作。 - quemeful
3个回答

7
常量已经更改(参见文档)。在使用launchOptions中的任何值之前,您还需要解包它。

为了更好地理解上下文,附上包含函数。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if let launchOptions = launchOptions {
        if #available(iOS 9.0, *) {
            if let shortcutItem = launchOptions[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
                print("Shortcut: \(shortcutItem)")
            }
        }
    }
    return true
}

1
如果这段代码在application(_:willFinishLaunchingWithOptions:)中,那么launchOptions字典已经知道其键类型为UIApplicationLaunchOptionsKey。所以你可以直接使用launchOptions[.shortcutItem] - rickster
你能否更新你的问题,包括代码和它所在的函数?最好将其作为内联代码粘贴,而不是截图。 - Naftali Beder
1
我的问题是迁移工具没有改变launchOptions类型。因此,基本上要检查UIApplicationDelegate的正确方法接口。应该是launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil - ricardopereira

1
启动选项字典类型在函数参数中已更改为[UIApplicationLaunchOptionsKey: AnyObject]。
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: AnyObject]?) -> Bool {

    ...

}

0

试一下这个...我用Xcode8,swift3可以工作

    //Check for ShortCutItem
    if #available(iOS 9.0, *) {
        if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接