7

Recently in Xcode 8 beta 6 (8S201h), this has become a problem.

 UIApplicationLaunchOptionsShortcutItemKey

Here's the error :

enter image description here

Anyone else having this issue?

var performShortcutDelegate = true
if let shortcutItem = launchOptions[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
    print("ok")
    self.shortcutItem = shortcutItem
    performShortcutDelegate = false
}
return performShortcutDelegate
ricardopereira
  • 11,118
  • 5
  • 63
  • 81
Amit Kalra
  • 4,085
  • 6
  • 28
  • 44
  • Try using `guard` : http://stackoverflow.com/questions/33689933/ambiguous-reference-to-member-subscript-on-dictionary – Bista Aug 16 '16 at 05:20
  • no luck :( same error – Amit Kalra Aug 16 '16 at 16:58
  • So you're still getting the `ambiguous reference to member subscript` error? Your code looks correct as shown, so it might have something to do with the enclosing function. It's also possible you need to include the block `if #available(iOS 9.0, *) {}` around your shortcut code. More information/context would be helpful. :) – Naftali Beder Aug 17 '16 at 14:40
  • So I just realized you're not unwrapping `launchOptions`, meaning it's still of `Optional` type when you try to use it. You can't pull values from an Optional dictionary, because it's [not technically a dictionary](http://stackoverflow.com/questions/24096293/assign-value-to-optional-dictionary-in-swift). That's likely the issue. I've updated my answer to reflect this, and included the enclosing function as well. Let me know if it works! – Naftali Beder Aug 17 '16 at 14:48
  • this still isn't working with Xcode 8 GM – quemeful Sep 12 '16 at 11:39

3 Answers3

7

The constant has changed (see the documentation). You also need to unwrap launchOptions before using any values it contains.

Enclosing function is included for context.

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
}
Naftali Beder
  • 1,066
  • 1
  • 10
  • 27
  • 1
    If this is in [`application(_:willFinishLaunchingWithOptions:)`](https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623032-application), then the `launchOptions` dictionary is already known to have key type `UIApplicationLaunchOptionsKey`. So you can just use `launchOptions[.shortcutItem]`. – rickster Aug 16 '16 at 18:37
  • Can you update your question to include the code and the function it's inside? (Ideally, please paste it as inline code, not as a screenshot.) – Naftali Beder Aug 16 '16 at 19:29
  • 1
    My problem was the migration tool who missed changing the `launchOptions` type. So basically check the `UIApplicationDelegate` for the correct method interface. Should be `launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil`. – ricardopereira Aug 26 '16 at 16:54
1

The launchOptions Dictionary type has changed in the function parameters to [UIApplicationLaunchOptionsKey: AnyObject].

private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: AnyObject]?) -> Bool {

    ...

}
quemeful
  • 9,542
  • 4
  • 60
  • 69
0

Try this.. Its work for me using Xcode8 , swift3

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