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?

2
  • if you want to override an existing method, you will have to subclass instead of writing an extension Commented May 25, 2016 at 8:22
  • @simpleBob It's not an existing method, I haven't implemented it in my Obj-C class. Commented May 25, 2016 at 8:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.