使用UIApplicationShortcutItem时对成员'下标'的模糊引用

时间:2016-09-10 00:03:19

标签: ios swift swift3 3dtouch

我正在尝试将我的代码迁移到Swift 3,并遇到有关尝试处理3D Touch快捷方式的错误。

我收到以下错误

  

使用3D Touch Shortcut执行segue - 不明确的参考   会员'下标'

对于以下行

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool {

if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
        handleShortcutItem(shortcutItem)
}


}

我不知道为什么我会这样,其他人都知道吗?

以下是我处理快捷方式的方法

enum ShortcutItemType: String {
case First
case Second
case Third

init?(shortcutItem: UIApplicationShortcutItem) {
    guard let last = shortcutItem.type.components(separatedBy: ".").last else { return nil }
    self.init(rawValue: last)
}

var type: String {
    return Bundle.main.bundleIdentifier! + ".\(self.rawValue)"
}
}


fileprivate func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {

    if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {

     let rootNavController = rootViewController.childViewControllers.first as! UINavigationController

        let viewController = rootNavController.childViewControllers[1]

        switch shortcutItemType {
        case .First:
            viewController.performSegue(withIdentifier: "firstSegue", sender: self)
            break
        case .Second:
            viewController.performSegue(withIdentifier: "secondSegue", sender: self)
            break
        case .Third:

            //Segue to view controller from first then perform another segue to a modal view. 

            viewController.performSegue(withIdentifier: "thirdSegue", sender: self)
            break
        }
    }
}


func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    handleShortcutItem(shortcutItem)
}

1 个答案:

答案 0 :(得分:4)

application(_:didFinishLaunchingWithOptions:)的方法标题已更改为:

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)

符号UIApplicationLaunchOptionsShortcutItemKey已替换为UIApplicationLaunchOptionsKey.shortcutItem

这可能是另一个问题,但application(_:performActionFor:completionHandler:)需要有这个标题:

func application(_ application: UIApplication,
                 performActionFor shortcutItem: UIApplicationShortcutItem,
                 completionHandler: @escaping (Bool) -> Void)

尝试修复所有这些。