Swift3 WatchKit+Swift 3.1:无法覆盖已标记为不可用的“手动过滤”

Swift3 WatchKit+Swift 3.1:无法覆盖已标记为不可用的“手动过滤”,swift3,xcode8,watchkit,Swift3,Xcode8,Watchkit,在Swift 3.1之前,以下代码的编译和运行没有任何问题: import WatchKit import WatchConnectivity import Foundation import UserNotifications class InterfaceController: WKInterfaceController, WCSessionDelegate { override func handleAction(withIdentifier identifier: Strin

在Swift 3.1之前,以下代码的编译和运行没有任何问题:

import WatchKit
import WatchConnectivity
import Foundation
import UserNotifications

class InterfaceController: WKInterfaceController, WCSessionDelegate {

    override func handleAction(withIdentifier identifier: String?, for notification: UNNotification) {

    // Do something

    }
}
但是,在使用Swift 3.1和WatchKit 3.0版在Xcode 8.3中编译时,我遇到以下编译器错误:

无法覆盖已标记为不可用的“handleAction”

从代码中删除重写会导致另一个编译器错误:

重写声明需要一个“override”关键字

根据方法handleAction with UNNotification仍然是WKInterfaceController的一种有效的非弃用方法,因此上面的代码应该是有效的且可编译的。将WatchKit版本设置为3.1或3.2不会改变任何内容

有什么想法可以让它与Swift 3.1配合使用吗?

TL;DR:您需要使用UnuseNotificationCenterDelegate

你落后了两步,而苹果的API参考目前落后了一步

当前的在线API参考:

不赞成

使用手柄​行动​标识符:​用于:​ 相反

但这也被弃用,如果打开WKInterfaceController类定义,您将发现以下第238行:

// deprecated
@available(watchOS 2.0, *)
@available(watchOS, deprecated: 3.0, message: "use UNUserNotificationCenterDelegate")
open func handleAction(withIdentifier identifier: String?, forRemoteNotification remoteNotification: [AnyHashable : Any])

@available(watchOS 2.0, *)
@available(watchOS, deprecated: 3.0, message: "use UNUserNotificationCenterDelegate")
open func handleAction(withIdentifier identifier: String?, for localNotification: UILocalNotification)
正如您所看到的,这也是不推荐的,但它指向了正确的方向:使用UNUserNotificationCenterDelegate。 换言之,您可以从API引用中认为您正在使用的方法不是不推荐的,但确实是

这与XCode更新有关,我认为它与Swift 3.1没有任何关系。

TL;DR:您需要使用UnuseNotificationCenterDelegate

你落后了两步,而苹果的API参考目前落后了一步

当前的在线API参考:

不赞成

使用手柄​行动​标识符:​用于:​ 相反

但这也被弃用,如果打开WKInterfaceController类定义,您将发现以下第238行:

// deprecated
@available(watchOS 2.0, *)
@available(watchOS, deprecated: 3.0, message: "use UNUserNotificationCenterDelegate")
open func handleAction(withIdentifier identifier: String?, forRemoteNotification remoteNotification: [AnyHashable : Any])

@available(watchOS 2.0, *)
@available(watchOS, deprecated: 3.0, message: "use UNUserNotificationCenterDelegate")
open func handleAction(withIdentifier identifier: String?, for localNotification: UILocalNotification)
正如您所看到的,这也是不推荐的,但它指向了正确的方向:使用UNUserNotificationCenterDelegate。 换言之,您可以从API引用中认为您正在使用的方法不是不推荐的,但确实是


这与XCode更新有关,我不认为它与Swift 3.1有任何关系。

多亏了Nycen,他促使我再次查看UNUserNotificationCenterDelegate,我最终在WatchKit项目的ExtensionLegate中实现了UNUserNotificationCenterDelegate,要处理根控制器中的不通知,请执行以下操作:

import WatchKit
import UserNotifications

class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {

    func applicationDidFinishLaunching() {
        UNUserNotificationCenter.current().delegate = self
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if let controller = WKExtension.shared().rootInterfaceController as? InterfaceController {
            // Call to a custom method in the root interface controller to handle the notification
            controller.handleAction(notification: response.notification)
            completionHandler()
        }
    }
}

感谢Nycen促使我再次查看UNUserNotificationCenterDelegate,我最终在WatchKit项目的ExtensionLegate中实现了UNUserNotificationCenterDelegate,以处理根控制器中的不通知,如下所示:

import WatchKit
import UserNotifications

class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {

    func applicationDidFinishLaunching() {
        UNUserNotificationCenter.current().delegate = self
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if let controller = WKExtension.shared().rootInterfaceController as? InterfaceController {
            // Call to a custom method in the root interface controller to handle the notification
            controller.handleAction(notification: response.notification)
            completionHandler()
        }
    }
}

这两个不推荐使用的方法是旧方法,最后一个基于UILocalNotification。它们与我问题中的方法不匹配。换句话说,这两个弃用警告并不能证明建议的基于UNNotification而非UILocalNotification的新方法也被弃用。最后两个方法甚至比引用的第一个与您的签名匹配的方法更加弃用,与答案无关:使用UnuseNotificationCenterDelegatei我在末尾添加了一行来澄清这一点。这两个不推荐使用的方法是旧方法,最后一个基于UILocalNotification。它们与我问题中的方法不匹配。换句话说,这两个弃用警告并不能证明建议的基于UNNotification而非UILocalNotification的新方法也被弃用。最后两个方法甚至比引用的第一个与您的签名匹配的方法更加弃用,与答案无关:使用UnuseNotificationCenterDelegates我在结尾添加了一行来澄清这一点。不过我对我的答案并不太喜欢。可能投票和/或接受@Nycen我看不到任何地方有确凿的证据支持你的说法,即未经通知的“手动过滤”已被弃用。除了它给出了一个编译器错误,但这可能是苹果方面的一个错误。更进一步说,我刚刚实现了代表。你的问题是,有什么想法可以让它与Swift 3.1一起工作?。感谢我回答的第一行,UNUserNotificationCenterDelegate。这不太公平。不过我不太喜欢我的答案。可能投票和/或接受@Nycen我看不到任何地方有确凿的证据支持你的说法,即未经通知的“手动过滤”已被弃用。除了它给出了一个编译器错误,但这可能是苹果方面的一个错误。更进一步说,我刚刚实现了代表。你的问题是,有什么想法可以让它与Swift 3.1一起工作?。感谢我回答的第一行,UNUserNotificationCenterDelegate。这不太公平。