我在台风中使用 swift,不知何故我的组件没有通过属性注入被注入。Strings
对于像它这样工作的简单类型。我提供了一个简单的例子来解释这个问题。输出显示对serviceB
有空引用的结果serviceA
。所有String
属性均已正确设置,不会引发错误。我在这里做错了什么?
XCode:6-beta5,台风:2.1.0
MYServiceA.swift
@objc(MYServiceA) public class MYServiceA : NSObject {
public var text : String!
}
MYServiceB.swift
@objc(MYServiceB) public class MYServiceB : NSObject {
public var text : String!
public var serivceA : MYServiceA!
}
MYAssembly.swift
public class MYAssembly : TyphoonAssembly {
public func serviceA() -> AnyObject {
var definitionBlock : TyphoonDefinitionBlock = {(definition : TyphoonDefinition!) in
definition.injectProperty("text", with: "some a text")
definition.scope = TyphoonScopeSingleton
}
return TyphoonDefinition.withClass(NSClassFromString("MYServiceA"), configuration: definitionBlock)
}
public func serviceB() -> AnyObject {
var definitionBlock : TyphoonDefinitionBlock = {(definition : TyphoonDefinition!) in
definition.injectProperty("text", with: "some b text")
definition.injectProperty("serivceA", with: self.serviceA())
definition.scope = TyphoonScopeSingleton
}
return TyphoonDefinition.withClass(NSClassFromString("MYServiceB"), configuration: definitionBlock)
}
}
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
var assembly : MYAssembly = MYAssembly()
var factory : TyphoonComponentFactory = TyphoonBlockComponentFactory(assembly: assembly) as TyphoonComponentFactory
factory.makeDefault()
var serviceA : MYServiceA = TyphoonComponentFactory.defaultFactory().componentForKey("serviceA") as MYServiceA
println("MYServiceA")
println("- instance=\(serviceA != nil)")
println("- text=\(serviceA.text)")
var serviceB : MYServiceB = TyphoonComponentFactory.defaultFactory().componentForKey("serviceB") as MYServiceB
println("MYServiceB")
println("- instance=\(serviceB != nil)")
println("- text=\(serviceB.text)")
println("- serviceA.instance=\(serviceB.serivceA != nil)")
return true
}
..
}
输出
MYServiceA
- instance=true
- text=some a text
MYServiceB
- instance=true
- text=some b text
- serviceA.instance=false