简体   繁体   English

如何通过Swift扩展实现委托方法

[英]How do I implement delegate methods through Swift extension

My project is a mix of Obj-C and Swift, and I'm trying to extend my AppDelegate class with Swift. 我的项目是Obj-C和Swift的混合,我正在尝试用Swift扩展我的AppDelegate类。

So right now I have AppDelegate.m , AppDelegate.h , and AppDelegate.swift . 所以现在我有AppDelegate.mAppDelegate.hAppDelegate.swift Most of the methods are in the Obj-C file, but I'm trying to implement just one of the methods: 大多数方法都在Obj-C文件中,但我试图只实现其中一种方法:

extension AppDelegate {

    func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {

    }
}

However it's complaining: 然而它抱怨:

AppDelegate.swift:11:10: Method 'application(_:continueUserActivity:restorationHandler:)' with Objective-C selector 'application:continueUserActivity:restorationHandler:' conflicts with previous declaration with the same Objective-C selector

Is this a limitation of Swift extension? 这是Swift扩展的限制吗? Or is there a way to implement this delegate method in my Swift extension file? 或者有没有办法在我的Swift扩展文件中实现这个委托方法?

You cannot do something like that. 你不能做那样的事情。 AppDelegate has defined this method ( UIApplicationDelegate protocol says so). AppDelegate定义了这个方法( UIApplicationDelegate协议说明了这一点)。

The simplest solution would be to rewrite AppDelegate in swift. 最简单的解决方案是在swift中重写AppDelegate

You have also more difficult solution - remove UIApplicationDelegate from your AppDelegate and implement this protocol in your extension. 您还有更难的解决方案 - 从您的AppDelegate删除UIApplicationDelegate并在您的扩展中实现此协议。 It may raise unexpected errors if your AppDelegate is considerable size. 如果您的AppDelegate尺寸相当大,可能会引发意外错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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