- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有一个 UITableViewController
,它在 didSelectRowAtSection
上加载了一个名为即 ClassToInject
的类的实例,它希望通过一个属性注入(inject),因为我们的 ViewControllerToBePushed
有一个属性 ClassToInject
,随后(因为它是一个 UITabBarViewController
)在 didSet
回调它搜索所有符合 ClassToInjectPresentable
的 viewControllers
属性,简单如下:
protocol ClassToInjectPresentable {
var property: ClassToInject { get set }
}
到现在为止,我只会做这样的事情:
func didSelectRowAtIndexPath {
let classToInject = self.loadClassToInjectFor(indexPath)
let tabBarViewController = SomeTabBarViewController()
tabBarViewController.property = classToInject
self.navigationController.push(tabBarViewController, animated: true)
}
并且在 SomeTabBarViewController
...
class SomeTabBarViewController: ClassToInjectPresentable {
var property: ClassToInject? {
didSet(newValue) {
self.viewControllers.filter{ $0 is ClassToInjectPresentable }.map{ $0 as! ClassToInjectPresentable }.forEach{ $0.property = newValue }
}
}
所有的东西都应该很容易地加载(但事实并非如此)。我读过有关 Swinject
的文章,这可能会用它来解决。我已经看到很多注册示例,例如:
container.register(Animal.self) { _ in Cat(name: "Mimi") }
但我不知道我是否可以注册一些在self
中加载的属性:
container.register(ClassToInjectInjector.self) { _ in
self.loadClassToInjectFor(indexPath) }
// And then
container.register(ClassToInjectPresentable.self) { _ in
SomeTabBarViewController() }
.initCompleted { r, p in
let tabBar = p as! SomeTabBarViewController
tabBar.property = r.resolve(ClassToInjectInjector.self)
// And lastly?
self.navigationController.pushViewController(tabBar, animated: true)
}
}
最佳答案
在不了解您的应用细节的情况下很难推荐合适的解决方案,但这里有一些建议:
container.register(ClassToInjectInjector.self) { _ in
self.loadClassToInjectFor(indexPath)
}
一般来说,所有的register
操作都应该在你的对象之外完成。常见的设置是拥有一个全局 Container
,其中包含所有注册 - 您应该将它们视为在没有任何隐式上下文的情况下构建应用程序对象的说明。如果您的依赖项需要在 UITableViewController
中创建,您可以将其作为参数传递给 resolve
方法:
container.register(ClassToInjectPresentable.self) { resolver, property in
let tabBar = SomeTabBarViewController()
tabBar.property = property
return tabBar
}
// in UItableVIewController
container.resolve(ClassToInjectPresentable.self,
argument: self.loadClassToInjectFor(indexPath))
这通常也是个坏主意:
.initCompleted { r, p in
...
self.navigationController.pushViewController(tabBar, animated: true)
}
您不应将应用程序逻辑与 DI 混合使用 - 纯粹使用 Swinject 来构建您的依赖项。
所以你的 UITableViewController
可能看起来像这样:
func didSelectRowAtIndexPath {
let classToInject = self.loadClassToInjectFor(indexPath)
let tabBar = container.resolve(
SomeTabBarViewController.self, argument: loadClassToInjectFor(indexPath)
)
navigationController.push(tabBar, animated: true)
}
至于您的 TabBar
及其 View Controller :UIViewControllers
如何进入 TabBar
?有可能做这样的事情吗?
class SomeTabBarViewController {
init(viewControllers: [UIViewController]) {
...
}
}
container.register(SomeTabBarViewController.self) { r, property
SomeTabBarViewController(viewControllers:[
r.resolve(MyViewController.self, argument: property),
r.resolve(MyViewController2.self, argument: property)
])
}
关于ios - Swinject 将自己的属性注入(inject)新的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356781/
我正在使用很棒的 Swinject对于我的项目中的 DI,但我在没有程序集的情况下使用它(当我开始使用 Swinject 时,还没有程序集)。 我目前的使用模式Containers是: class P
我正在寻找一种方法来测试服务是否已在 .Container 范围内解析。我的应用程序在启动时启动了一些“服务”,我想确保这些服务已得到解决,而不触发它们被解决。 最佳答案 您可以在工厂闭包中添加日志记
我在我的 swift 项目中使用 Swinject 进行 DI。我认为使用弱对象作用域会导致泄漏。 我有这个虚拟对象 protocol DevTestProtocol: class {} class
在尝试将某些协议(protocol)绑定(bind)到各种类时,我遇到了一种奇怪的行为,即在协议(protocol)中获取 nil。这是依赖注入(inject)器类: class Dependency
我正在尝试创建一个函数来返回一个元类型数组,然后我可以使用它来解析来自 Swinject Resolver 的实例。这是我的代码: protocol Task: class { } func getT
我在我的 Swift iOS 应用程序中使用 Swinject,并尝试按照官方文档页面上的说明进行循环依赖:https://github.com/Swinject/Swinject/blob/mast
我有一个问题,我在单独的框架中使用 Swinject。 我的应用从这个框架获取对象,我的 appExtension 也使用它。 但在扩展中,对象没有正确实例化。 Swinject 是使用我的对象的第一
我正在使用 Swinject在我的 Swift 3 应用中。当我尝试时 let container = Container() container.register(NetworkModeling.s
目前我这样做: container.register(TabBarViewModelable.self) { (_, tabBarItems: [TabBarItemType]) in
每当我运行此代码时,VCModel 的 init() 都会被调用,但 Swinject 不会将 VCModel 实例注入(inject)到我的 ViewController 中。有人可以告诉我我做错了
我使用 SWInject 来维护我的应用程序中的依赖关系,并使用协调器模式来管理逻辑和依赖关系。 SWInject 能否与应用程序不同部分的多个观察者的集中式推送通知框架结合使用? 通常,我使用 Si
我开始在我的应用程序中使用 Swinject,我想在 presenter(Viper/MVP) 或 viewControllers(不是毒蛇)。我遇到的主要问题是我想在 presenters 中共享
import Swinject class GenericsAssembly: Assembly { func assemble(container: Container) {
此时,在我的项目中,我使用 Swinject(DI 容器)。 如果我有以下层次结构,我想知道如何注册 ViewController: class RateAnswersBaseVC: UIViewCo
是否可以解决/创建未在 Swinject 容器中注册的对象的新实例?在 C#(来自 Microsoft)的 Unity 依赖注入(inject)中就是这样。 我想解析 viewModel 类,它依赖于
假设我们有一个 UITableViewController,它在 didSelectRowAtSection 上加载了一个名为即 ClassToInject 的类的实例,它希望通过一个属性注入(inj
我正在尝试使用 Swinject 注入(inject)依赖项,但我不知道自己做错了什么。 我有处理注册字符串用户的协议(protocol)。 protocol AuthServiceProtocol
我有一个测试项目,我试图将参数传递给 Swinject 中的 resolve 方法项目。 这是我的 Swinject Storyboard扩展文件中包含的示例。 import Swinject ext
这个问题是给有很多Swinject经验的人的对于 swift 。 我会展示有问题的代码,我的问题在底部。 代码有点多,抱歉。 这是 MySwinjectStoryboard.swift 注册: imp
我正在使用 Swinject 来注入(inject)依赖项,到目前为止效果很好。但现在我有一个场景,其中我有一个 ViewController,它依赖于它的 ViewModel。 ViewModel
我是一名优秀的程序员,十分优秀!