台风依赖注入和 Swift 3:Appdelegate 不是 AnyObject
Typhoon Dependency Injection and Swift 3: Appdelegate isn't AnyObject
此代码适用于 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())
}
}
...
}
但是,对于预期 return 'AnyObject' 的任何 Swift 3 文件,ApplicationAssembly 中会显示以下错误:
“没有 'withClass' 候选人产生预期的上下文结果类型 'AnyObject'
可能有人了解 Obj-c Typhoon 代码库与 Swift 3 的不兼容性吗?
Screen capture of error line
您可能希望将 return 类型从 AnyObject
切换为 Any
。
withClass
函数return是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.
此代码适用于 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())
}
}
...
}
但是,对于预期 return 'AnyObject' 的任何 Swift 3 文件,ApplicationAssembly 中会显示以下错误: “没有 'withClass' 候选人产生预期的上下文结果类型 'AnyObject'
可能有人了解 Obj-c Typhoon 代码库与 Swift 3 的不兼容性吗?
Screen capture of error line
您可能希望将 return 类型从 AnyObject
切换为 Any
。
withClass
函数return是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.