Clean VIP with Delegate Pattern



我是Clean VIP Architecture的新手,我正在为它的切入点而挣扎。

(我只放了一些代码)

ViewController

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
}

现在我面临的问题是,TilesConfigurator之外没有interactor的参考,delegateweak,这意味着它的总arc为0。结果是viewDidLoad里面有delegate = nil

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

p。S:我不认为在ViewController内部对委托做一个强引用是好的做法

Delegate不应该是weak

var delegate: Delegate?

因为有一部分是weak也就是let interactor = Interaction()所以不会出现保留循环

最新更新