swift-ioc-container
import Swift_IoC_Container
protocol SuperAwesomeBot {
func saySomething()
}
class StarWarsBot: SuperAwesomeBot {
func saySomething() {
print("The Force will be with you. Always.")
}
}
您可以使用如下方式使用它
// only needed once
IoC.shared.registerLazySingleton(SuperAwesomeBot.self, { StarWarsBot() })
let myBot: SuperAwesomeBot = try! IoC.shared.resolve()
myBot.saySomething()
如果您使用的是 Swift 5.1 或更高版本,可以充分利用新的属性包装功能。此库已经实现了该功能。
无需在初始化器中手动初始化属性,只需声明具有预期协议类型的属性即可。
@Injected private var myBot: SuperAwesomeBot
将属性声明为 @Injected
之后,即可像以往一样访问之前注册的对象。
示例实现可以在 Example.swift 中找到
需求
除 Swift 5.1 外没有其他需求,只需将这个小库复制到您的项目中并开始使用。
安装
swift_ioc_container 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod 'Swift-IOC-Container'
更新
- 首先,进行更改:)
- 将 PodSpec 更新为新版本号
- 推送并创建一个新的标签,标签号与版本号相同
- 执行以下行以在 CocoaPods.org 上更新 PodSpec
pod trunk push Swift-IOC-Container.podspec
注意
当然,您需要是所有者。
- 创建您的账户如下
pod trunk register [email protected] "Mein Name" --description="Name meines Rechners"
- 所有者需要将您添加为所有者
pod trunk add-owner Swift-IOC-Container [email protected]
故障排除
与 .netRc 出错
chmod 0600 ~/.netrc
作者
Michael Scherbakow, [email protected]
Jonas Österle, [email protected]
Pascal Friedrich, [email protected]
Philipp Manstein, [email protected]
许可
Swift_IoC_Container 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。