3

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?

4

2 回答 2

5

该方法的签名现在是:

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

请注意,完成处理程序现在是@escaping,根据SE-103(将非转义闭包设为默认值)。这个属性改变了闭包参数的类型签名,进而改变了它作为参数的方法的类型签名,所以旧声明的方法不会被调用。

一般来说,编译器警告/修复对于捕获所有类型签名更改并不是那么好,尤其是在 beta 之间。您最好的选择是返回 SDK 标头(或者更确切地说,从它生成的 Swift 接口)或 Apple 网站上的文档/Xcode 中定义问题方法的类/协议,以便您可以看到它的新定义是什么。

于 2016-08-16T17:58:45.853 回答
1

Apple@escaping在 Swift 3 Beta 6 中引入了这个标签。现在所有的闭包都默认没有转义,所以如果你想要一个转义的闭包,你需要给出那个标签。出于某种原因,swift translate 没有添加此标签,但根据下面链接中的文档,您需要在关闭之前添加此标签。

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

将闭包添加到我的代码中删除了警告:

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

我没有测试它,所以它可能只是出于其他原因删除了警告。

于 2016-08-16T17:48:20.553 回答