2

我正在尝试将 VIPER 架构与 SwiftUI 一起使用,使用一些我已经在 UIKit 中使用的协议。

这些是我的BasePresenterAssembler实现:

class BasePresenter <V, I, R>: NSObject, Presenter {
    typealias View = V
    typealias Interactor = I
    typealias Router = R
    
    var view: View!
    var interactor: Interactor!
    var router: Router!
    
    func viewDidLoad() {
           print("Base presenter: Viewdidload")
       }
}

protocol Assembler {
    associatedtype CustomView: ViewProtocol
    associatedtype CustomInteractor: Interactor
    associatedtype CustomPresenter: Presenter
    associatedtype CustomRouter: Router
    static func assembleView(_ view: CustomView) -> CustomView
    
}


extension Assembler {
    static func assembleView(_ view: CustomView) -> CustomView {
        let presenter = CustomPresenter.init()
        var view = view
        
        presenter.view = (view as! CustomPresenter.View)
        view.presenter = (presenter as! CustomView.Presenter)
        
        let interactor = CustomInteractor.init()
        interactor.presenter = (presenter as! CustomInteractor.Presenter)
        presenter.interactor = (interactor as! CustomPresenter.Interactor)
        
        let router = CustomRouter.init()
        router.presenter = (presenter as! CustomRouter.Presenter)
        presenter.router = (router as! CustomPresenter.Router)
        
        return view
    }
}

在我想添加环境对象之前,它与 SwiftUI 视图一起工作得很好。

问题是,如果我以这种方式添加它:

LoginAssembler.assembleView(LoginView()).environmentObject(user)

当我尝试修改它时应用程序崩溃

作为该视图的祖先,可能缺少一个View.environmentObject(_:)forUserModel

我试图将环境对象注入更改为视图的初始化,但是它不再是类型LoginView(它是ModifiedContent<...>)并且汇编程序崩溃了。

let view = view.init().environmentObject(environmentObject)
let presenter = CustomPresenter.init()
print(view is LoginView) // Prints false

知道我可以用什么来组装我的模块并使 environmentObject 选项起作用吗?

4

0 回答 0