简体   繁体   中英

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?

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.

Apple introduced the @escaping tag to the Swift 3 Beta 6. All closures now are by default no escaping, so if you want a closure that escapes, you need to give that tag. For some reason swift translator did not add this tag, but according to documentation in the link below, you need this tag added before the closure.

https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622935-application

Adding the closure to my code removed the warning:

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

I did not test it, so it might just removed the warning for some other reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM