gpt4 book ai didi

ios - Swift Typhoon 组件属性注入(inject)不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:57 25 4
gpt4 key购买 nike

我在 typhoon 中使用 swift,不知何故我的组件没有通过属性注入(inject)注入(inject)。对于像 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

最佳答案

Typhoon 程序集充分利用了 ObjC 运行时的动态调度功能。程序集被反射,每个方法被拦截(应用 AOP“周围”建议),这样 Typhoon 就有了一个蓝图来构建应用程序程序集中的每个组件。然而,Swift 会尽可能地尝试使用静态/vtable 调度(这可以防止所需的方法拦截)。

要指示 Swift 需要动态分派(dispatch),请将您的程序集方法标记为“动态”。

示例:(需要 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)
}

}

关于ios - Swift Typhoon 组件属性注入(inject)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com