3

我在台风中使用 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
4

1 回答 1

5

Typhoon 程序集充分利用了 ObjC 运行时的动态调度特性。程序集被反射,每个方法被拦截(应用 AOP 'around' 建议),以便 Typhoon 有一个蓝图来构建应用程序程序集中的每个组件。但是,Swift 会尽可能地尝试使用静态/vtable 调度(这会阻止所需的方法拦截)。

要指示 Swift 需要动态调度,请将您的组装方法标记为“动态”。

示例:(需要 Xcode6 beta 6)

public class MYAssembly : TyphoonAssembly {

    public dynamic func serviceA() -> AnyObject {
        var definitionBlock : TyphoonDefinitionBlock = {
            (definition : TyphoonDefinition!) in

            definition.injectProperty("text", with: "some a text")
            definition.scope = TyphoonScopeSingleton
        }
        return TyphoonDefinition.withClass(MYServiceA.classForCoder(), 
            configuration: definitionBlock)
    }

    public dynamic 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(MYServiceB.classForCoder(), 
            configuration: definitionBlock)
    }

}
于 2014-08-27T22:42:39.000 回答