Search code examples
iosxcodeswift3uiapplicationdelegateuiapplicationshortcutitem

AppDelegate function for UIApplicationShortcutItem not being called in Swift 3 / Xcode 8 beta 6


The Swift 3 converter changed this (perfectly functioning) line:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

to this:

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

but both produce the warning

Instance method 'application(:handleActionWithIdentifier:for:completionHandler:)' nearly matches optional requirement 'application(:handleActionWithIdentifier:for:completionHandler:)' of protocol 'UIApplicationDelegate'

and offer the solution of making the function private, or adding @nonobjc.

Whether the function is left with the warning, reverted to the Swift 2 syntax, or fixed in either suggested way, launching the app with a shortcut item does not trigger it.

This is not listed as a known issue here either. Does anybody have an idea?


Solution

  • The signature for that method is now:

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

    Note the completion handler is now @escaping, per SE-103 (Make non-escaping closures the default). This attribute changes the type signature of the closure parameter, which in turn changes the type signature of the method it's an argument to, so method with the old declaration won't be called.

    In general, the compiler warnings/fixits aren't so great for catching all type signature changes, especially between betas. Your best bet is to return to the SDK header (or rather, the Swift interface generated from it) or the documentation on Apple's site / in Xcode for the class/protocol that defines a problem method so you can see what its new definition is.