此代码适用于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的不兼容性吗?
答案 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.