Search code examples
iosswiftswift33dtouch

Ambiguous Reference To Member 'Subscript' when using UIApplicationShortcutItem


I am trying to migrate my code to Swift 3, and came across an error regarding trying to handle 3D Touch shortcuts..

I get the following error

Performing segue using 3D Touch Shortcut - Ambiguous Reference To Member 'Subscript'

For the following line

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

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


}

I am not sure why I am getting this, does anyone else know ?

Here's how I am handling the shortcuts

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)
}

Solution

  • The method header of application(_:didFinishLaunchingWithOptions:) has changed to:

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

    The symbol UIApplicationLaunchOptionsShortcutItemKey has been replaced with UIApplicationLaunchOptionsKey.shortcutItem.

    And this may be another issue, but application(_:performActionFor:completionHandler:) needs to have this header:

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

    Try fixing all of them.