2

My project is a mix of Obj-C and Swift, and I'm trying to extend my AppDelegate class with Swift.

So right now I have AppDelegate.m, AppDelegate.h, and AppDelegate.swift. Most of the methods are in the Obj-C file, but I'm trying to implement just one of the methods:

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? Or is there a way to implement this delegate method in my Swift extension file?

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156

1 Answers1

4

You cannot do something like that. AppDelegate has defined this method (UIApplicationDelegate protocol says so).

The simplest solution would be to rewriteAppDelegate in swift.

You have also more difficult solution - remove UIApplicationDelegate from your AppDelegate and implement this protocol in your extension. It may raise unexpected errors if your AppDelegate is considerable size.

Damian Rzeszot
  • 548
  • 3
  • 12
  • Ok, let's say this is a view controller instead with a `TableViewDelegate` methods that needs to be implemented. Can I extend my view controller and implement those methods there instead? If so how come it works in that scenario but not here? (P.S. My app delegate is not that big anyway so it's not a big deal to rewrite it in Swift) – Enrico Susatyo May 25 '16 at 08:32
  • It will work if you have `SomeViewController ` (subclass of `UIViewController `) and extension for this VC with protocol `UITableViewDelegate`. But it will fail if you create `SomeViewController` (subclass of `UIViewController`) and `UITableViewController` protocol and then you create extension for `SomeViewController` with methods described in extension. – Damian Rzeszot May 25 '16 at 09:07