简体   繁体   English

自我解雇期间,弱者,强者或无主者都不会做吗?

[英]Neither weak nor strong or unowned will do during self dismissal?

Is this a valid pattern to avoid being retained after the dismissal? 这是避免被解雇后保留的有效模式吗?

@objc func backButtonTapped() {
    var s: SiteViewController! = self
    navigationController!.popToRootViewController(animated: true, completion: {
        s.dismissCompletion()
        s = nil // break the strong reference to self
    })
}

You can just use self in the closure. 您可以只在封闭中使用self Once control returns from the closure the reference to self will be released. 一旦控件从关闭返回,对self的引用将被释放。

If you did want to use s there is no reason to declare it as an implicitly unwrapped optional. 如果确实要使用s ,则没有理由将其声明为隐式展开的可选。 You could just say var s = self 你可以说var s = self

This closure should release s right once it is executed. 此次关闭应该释放s一旦执行权。 I think you can place self there without worries 我想你可以self在这里

You can do following to avoid retention, 您可以执行以下操作以避免保留,

@objc func backButtonTapped() {
  navigationController!.popToRootViewController(animated: true, completion: { [weak self] in
       if let strongSelf: SiteViewController = self {
         strongSelf.dismissCompletion()
       } 
   })
}

This will make sure there won't be any retention. 这样可以确保不会有任何保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM