使用委托模式清理 VIP

问题描述

我是 Clean VIP Architecture 的新手,我正在努力寻找它的入口点。

(我只是放了一些代码

视图控制器

protocol Delegate: class { 
   func execute()
}

class TitlesViewController:UIViewController {
   weak var delegate: Delegate?

   func viewDidLoad() {
         super.viewDidLoad()
         delegate.execute()
      }

}

配置器

class TitlesConfigurator {

static func configureModule(viewController: TitlesViewController) {
    let interactor = Interaction()
    
    viewController.delegate = interactor
    
   }
}

在 AppDelegate 中

func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let titlesViewController = TitlesViewController()
    let navigationController = UINavigationController(rootViewController: titlesViewController)
    TitlesConfigurator.configureModule(viewController: titlesViewController)
    
    window = UIWindow()
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()
    
    return true
}

现在我面临的问题是在 interactor 之外没有 TilesConfigurator 的引用,delegateweak,这意味着它的总 arc 是0. 它导致 delegate = nilviewDidLoad

如何改进或修复我的架构中的这个问题。

P.S:我认为在 ViewController

中强引用委托不是一种好的做法

解决方法

代表不应weak在这里

var delegate: Delegate?

因为有一部分是 weaklet interactor = Interaction() 所以不会发生保留循环