iosobjective-cswiftmigrationtyphoon

Typhoon Dependency Injection and Swift 3: Appdelegate isn't AnyObject


This code works with Typhoon Dependency Injection library (Obj-C) in Swift 2.3 but doesn't in Swift 3:

AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var rootViewController: RootViewController?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = self.rootViewController
        self.window?.makeKeyAndVisible()

        return true
    }
    ...
}

Application Assembly:

    public class ApplicationAssembly: TyphoonAssembly {

        public dynamic func config() -> AnyObject {
            return TyphoonDefinition.withConfigName("Configuration.plist")
        }

        public dynamic func appDelegate() -> AnyObject {
            return TyphoonDefinition.withClass(AppDelegate.self) {
                (definition) in

                definition!.injectProperty(#selector(ApplicationAssembly.rootViewController), with: self.rootViewController())
            }
        }
 ...
}

However the following error is displayed in ApplicationAssembly for any Swift 3 file expected to return 'AnyObject': "No 'withClass' candidates produce the expected contextual result type 'AnyObject'

Might anyone have an insight into the incompatibility of the Obj-c Typhoon code base with Swift 3?

Screen capture of error line


Solution

  • You may want to switch the return type from AnyObject to Any.

    The withClass function returns an id type in Objective-C, see the source code:

    + (id)withClass:(Class)clazz block:(TyphoonBlockDefinitionInitializerBlock)block;
    

    The id type used to be mapped to AnyObject in Swift 2, but in Swift 3 it's mapped to Any for increased flexibility. You can read more about this change here.