简体   繁体   English

Swift,闭包捕获了多少“自我”?

[英]Swift, how much of “self” do closures capture?

I am confused by this situation: 我对这种情况感到困惑:

  1. Class A hands an escaping closure Z to some global queue. A类将转义的闭包Z交给某个全局队列。

  2. In closure Z, we call funcs in class A, and class B who is only referenced from class A. 在闭包Z中,我们称A类为类,而B类仅从A类中引用。

  3. Just before closure Z calls functions from class B, class B gets cleaned out. 在闭包Z从类B调用函数之前,类B被清除了。

  4. What does class B mean to closure Z at this point? B级对此时的封闭Z意味着什么? Is it still the old B at time of capture or the new nil? 在捕获时还是旧的B还是新的Nil? (Assuming B was a delegate) (假设B是代表)

== ==

Another follow up question would be, if the funcs of class B called by closure Z reference a bunch more things in class B, how is the "self" of B captured, is it strong or weak? 另一个跟进问题是,如果闭包Z调用的B类函数在B类中引用了更多东西,那么B的“自我”是如何被捕获的呢?

My confusion comes because in closure Z, i can specify [weak self], but I cannot do that for the functions I want to call in class B. 我感到困惑的原因是,在闭包Z中,我可以指定[弱自我],但是对于要在类B中调用的函数,我不能这样做。

and class B who is only referenced from class A.

This means either two things: 这意味着两件事:

  1. B instance is a strong property, and will not be released at least until A instance gets released, and A instance will not get released until the end of the closure. B实例是一个很强的属性,至少在A实例被释放之前不会被释放,并且A实例直到闭包结束才被释放。

  2. B instance is a weak property, and might get released at any time ( A instance has nothing to say in this case). B实例是一个弱属性,可能随时被释放(在这种情况下, A实例无话可说)。 But at the same time it means B instance has to be optional therefore the compiler will force you to properly handle the scenario where it is deallocated (of course you can force unwrap it, but then it's your problem). 但是同时,这意味着B实例必须是可选的,因此编译器将迫使您正确处理被释放的情况(当然,您可以强制对其进行拆包,但这是您的问题)。

class B who is only referenced from class A. 仅从A类引用的B类。

I assume you mean something like this in A : 我认为您在A意思是这样A

var objB: B?

Just before closure Z calls functions from class B, class B gets cleaned out. 在闭包Z从类B调用函数之前,类B被清除了。

Well, there is only one way to clean out B : 好吧,只有一种方法可以清除B

objB = nil

Now when you call B's methods in the closure, you will get an "unexpectedly found nil while unwrapping an optional value". 现在,当您在闭包中调用B的方法时,您将得到“在展开一个可选值时意外发现nil”。

This is because you only captured self , which is A ,in the closure. 这是因为您仅在闭包中捕获了self ,即A You did not capture B at all. 您根本没有捕获到B The instance of B is retained by the strong reference from A . B的实例由A的强引用保留。 Basically, B is not captured by the closure. 基本上, B没有被闭包捕获。

My confusion comes because in closure Z, i can specify [weak self], but I cannot do that for the functions I want to call in class B. 我感到困惑的原因是,在闭包Z中,我可以指定[弱自我],但是对于要在类B中调用的函数,我不能这样做。

Well, you can capture B though. 好吧,虽然您可以捕获B Just like this: 像这样:

[b = self.objB] in ...

You can also make it weak: 您也可以使其变弱:

[weak b = self.objB] in ...

This way you can also capture B in the closure. 这样,您还可以捕获闭包中的B。

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

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