在尝试和玩弄Typhoon DI时,我意识到LazySingleton作用域并不像预期那样工作,这意味着甚至在使用它们之前注入了惰性属性。作为更具体的我创建了一个TyphoonAssembly如下:Swift中的台风懒惰注射
public class AppAssembly : TyphoonAssembly {
public dynamic func knight() -> AnyObject{
return TyphoonDefinition.withClass(Knight.self){
(definition) in
definition.injectProperty("name", with: "Dragon")
definition.injectProperty("horse")
definition.scope = TyphoonScope.LazySingleton
}
}
public dynamic func horse() -> AnyObject{
return TyphoonDefinition.withClass(Horse.self){
(definition) in
definition.injectProperty("color", with: "red")
definition.scope = TyphoonScope.LazySingleton
}
}
}
其中骑士是NSObject的,具有validateProperties
功能
class Knight:NSObject {
var name:String?
var horse: Horse?
func validateProperties(){
if name != nil{
println("Name not nil")
}
if horse != nil{
println("Horse not nil")
}
}
}
激活装配和正从它的骑士后,调用validateProperties
功能总是打印名称不是零和即使这些属性从未在我的代码中使用过,它也不是零。我在这里丢失了什么,或者懒惰的注入与Swift懒惰存储的属性不一样吗?