Typhoon Dependency Injection和Swift 3:Appdelegate不是AnyObject

时间:2017-03-08 17:24:57

标签: ios objective-c swift migration typhoon

此代码适用于Swift 2.3中的Typhoon依赖注入库(Obj-C),但不适用于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
    }
    ...
}

申请大会:

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

但是,对于任何期望返回“AnyObject”的Swift 3文件,ApplicationAssembly中会显示以下错误: “没有'withClass'候选人产生预期的上下文结果类型'AnyObject'

任何人都可以深入了解Obj-c Typhoon代码库与Swift 3的不兼容性吗?

Screen capture of error line

1 个答案:

答案 0 :(得分:0)

您可能希望将返回类型从AnyObject切换为Any

withClass函数返回Objective-C中的id类型 see the source code

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

id类型曾在Swift 2中映射到AnyObject,但在Swift 3中,它已映射到Any以提高灵活性。 You can read more about this change here.