4

我在一个快速项目中使用台风,据我所知,我必须像这样在 TyphoonAssembly 中明确映射所有注入:

 public dynamic func appDelegate() -> AnyObject {
    return TyphoonDefinition.withClass(AppDelegate.self) {
        (definition) in

        definition.injectProperty("cityDao", with:self.coreComponents.cityDao())
        definition.injectProperty("rootViewController", with:self.rootViewController())
    }
}

这似乎很难管理,而且非常脆弱(重构时)。

我看到这里支持自动注入(按类型匹配): https://github.com/appsquickly/Typhoon/wiki/Auto-injection-(Objective-C) 但这是针对目标 c。

有谁知道我可以连接注入而不用他们的名字显式注册道具作为字符串?

谢谢!

4

2 回答 2

1

(Typhoon creator here).

Typhoon is a dynamic, introspective dependency injection container , and uses the Objective-C run-time. There are the following limitations, when it comes to Swift:

  • With Objective-C it avoids magic strings, allowing the use of ordinary IDE refactoring tools, however in Swift selectors are magic strings.
  • The Objective-C runtime only provides type information for properties, not method or initializer parameters. So only properties can support auto-wiring of any kind (macros, implicit, etc).
  • There is no annotation or macro system for Swift, (but it does have 1st class functions).

You can instruct Typhoon to auto-wire a property in Swift using the following:

 public dynamic func appDelegate() -> AnyObject {
    return TyphoonDefinition.withClass(AppDelegate.self) {
        (definition) in

        definition.injectProperty("cityDao")
        definition.injectProperty("rootViewController")
    }
}

. . and this will match by type, just as the auto-wiring Objective-C macros do. However this does not avoid specifying the name of the property to be injected. There is no other way to do this in Swift. :(

Its worth mentioning there are additional limitations when using Typhoon with Swift:

  • "Pure" Swift classes - not extending a Cocoa base class or declaring the @objc directive - don't support introspection, dynamic dispatch or dynamic method invocation. Typhoon only works with Cocoa classes.
  • Swift protocols require the @objc directive.
于 2015-04-06T08:15:06.050 回答
1

可以在 Swift 中使用 Typhoon 的自动连线功能。

ObjC 中的自动装配是这样的:

@property (nonatomic, strong) InjectedClass(ViewController) rootViewController;

我们可以调查一下宏的定义,InjectedClass

#define InjectedProtocol(aProtocol) TyphoonInjectedObject<aProtocol>*
#define InjectedClass(aClass) aClass<TyphoonInjectedProtocol>*

因此,Swift 中的自动装配就像

var rootViewController: (ViewController & TyphoonInjectedProtocol)?
于 2019-09-20T08:18:15.173 回答