简体   繁体   English

我们需要担心闭包调用的弱内部方法吗?

[英]Do we need to worry about weak self inside methods called from closures

Trivial but looking for confirmation I'm understanding correctly: 琐碎,但正在寻找确认,我正确理解了:

If we have a simple closure as so: 如果我们有一个简单的闭包:

let closure = { [weak self] in 
    self?.doSomething()
}

Do we need to worry about weak/strong self within the method itself? 我们是否需要担心方法本身内部的弱/强自我?

func doSomething() {
    self.classString = "some new string" 
    // the self in this case will come from the weak self in the closure?
}

The following as I understand is unnecessary even though it'll be called from a closure: 据我了解,以下内容是不必要的,即使会从闭包中调用它:

func doSomething() {
    weak var weakSelf = self
    weakSelf.classString = "some new string" 
}

不,我们不必担心,因为一种方法无法捕获任何东西。

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

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