我尝试将VIP架构引入到我的项目中,并且不完全理解我找到的示例,我遵循此信息https://www.netguru.com/blog/clean-swift-ios-architecture-pattern,并检查内部的源代码,我对背后的想法感到困惑,这是创建此组件的可重用想法或如何构建此组件的方式之一?例子:
// Scene Factory
protocol SceneFactory {
var configurator: LoginSceneConfigurator! { get set }
func makeLoginScene() -> UIViewController
}
final class DefaultSceneFactory: SceneFactory {
var configurator: LoginSceneConfigurator!
func makeLoginScene() -> UIViewController {
let vc = LoginSceneViewController()
return configurator.configured(vc)
}
// Configurator
protocol LoginSceneConfigurator {
func configured(_ vc: LoginSceneViewController) -> LoginSceneViewController
}
final class DefaultLoginSceneConfigurator: LoginSceneConfigurator {
private var sceneFactory: SceneFactory
init(sceneFactory: SceneFactory) {
self.sceneFactory = sceneFactory
}
@discardableResult
func configured(_ vc: LoginSceneViewController) -> LoginSceneViewController {
sceneFactory.configurator = self
let service = DefaultAuthService(
networkManager: DefaultNetworkManager(session: MockNetworkSession())
)
let authWorker = LoginSceneAuthWorker(service: service)
let interactor = LoginSceneInteractor()
let presenter = LoginScenePresenter()
let router = LoginSceneRouter(sceneFactory: sceneFactory)
router.source = vc
presenter.viewController = vc
interactor.presenter = presenter
interactor.authWorker = authWorker
vc.interactor = interactor
vc.router = router
return vc
}
}
在我的情况下,我使用一个简单的小结构,不知道这是否是正确的方式,当我复制这个想法并试图在我的项目中实现它时,这不起作用。我还附上了我的项目在谷歌https://drive.google.com/file/d/1DcTxDXNl8idp2C3HLs5ggixdRVTE1UGY/view?usp=sharing的源代码我的例子:
protocol HomeViewProtocol {
func reloadView(_ bucketLists: [TestData])
func showSearchResult(result: String)
func progressState()
}
extension HomeViewController: HomeViewProtocol {
func progressState() {
views?.loader.startAnimating()
}
func showSearchResult(result: String) {
router?.showSearchMsg(result)
views?.loader.stopAnimating()
}
func reloadView(_ bucketLists: [TestData]) {
self.bucketLists = bucketLists
views?.label.text = bucketLists.last?.title
}
}
class HomeViewController: UIViewController {
private var bucketLists: [TestData] = []
var interactor: HomeInteractorProtocol?
var router: HomeViewControllerRouter?
var views: HomeViewContollerViews?
override func viewDidLoad() {
super.viewDidLoad()
configurator()
views?.setupUI()
views?.setupConstraint()
views?.setupNavigationControllerElemenst()
}
private func configurator() {
let searchWorker: HomeViewControllerSearchWorkerProtocol = HomeViewControllerSearchWorker()
var presenter: HomePresenterProtocol = HomePresenter()
presenter.viewController = self
interactor = HomeInteractor()
interactor?.presenter = presenter
interactor?.searchWorker = searchWorker
router = HomeViewControllerRouter()
router?.source = self
views = HomeViewContollerViews()
views?.source = self
}
}
我一直在研究这个问题,就像假设可以说最方便的方法是为配置创建一个静态函数一样,我想这是一种经典的方法,问题中描述的所有信息都是生成"配置器"的一种变体,我在文章中发现的一种方法,对我来说太复杂了,静态函数是经典的。这些想法是基于对VIP和VIPER架构的几个项目的研究,如果你有任何其他的想法或想法,我将不胜感激。我的项目与VIP架构和我发现的项目与VIPER架构将附上,如果你只是刚刚开始熟悉这种架构,这些项目将对你感兴趣
注:我没有在我的项目中删除的一小段代码也包含了一个依赖注入/反转的例子,但我想对某些人来说会很有趣,这些是Swift Book &Aleksey Chechil>我的代码:
class HomeVCConfigurator {
class func configure() -> HomeViewController {
let viewController = HomeViewController()
let searchWorker: HomeViewControllerSearchWorkerProtocol = HomeViewControllerSearchWorker()
var presenter: HomePresenterProtocol = HomePresenter()
presenter.viewController = viewController
viewController.interactor = HomeInteractor()
viewController.interactor?.presenter = presenter
viewController.interactor?.searchWorker = searchWorker
viewController.router = HomeViewControllerRouter()
viewController.router?.source = viewController
viewController.views = HomeViewContollerViews()
viewController.views?.source = viewController
return viewController
}
}
场景代表:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
let vc = HomeVCConfigurator.configure()
vc.id = 5
let navigatioContoller = UINavigationController(rootViewController: vc)
window.rootViewController = navigatioContoller// Your initial view controller.
window.makeKeyAndVisible()
self.window = window
}