简体   繁体   English

这是强还是弱?

[英]Is this strong or weak?

Suppose this situation: 假设这种情况:

__weak UIImage *image = [self calculateImage];

// a few lines later...

image = [self calculateImageUsingAnotherProcess];

Supposing calculateImage and calculateImageUsingAnotherProcess returning strong references. 假设calculateImagecalculateImageUsingAnotherProcess返回强引用。

OK, in the first case image is weak but what about the second? 好的,在第一种情况下image weak但是第二种情况又如何呢? Will image maintain the weak aspect? image保持weak吗?

Second one will stay weak as well. 第二个也将保持弱势。 It cannot be changed after the declaration. 声明后不能更改。

You can't return a strong reference in that sense. 从这种意义上讲,您不能返回强有力的参考。 It's the container where the reference is put that dictates whether it is strong, weak or otherwise. 引用所在的容器决定了它是强还是弱。 If you've declared a weak container then any reference you assign to it will be held weakly. 如果您声明了一个weak容器,那么您分配给它的任何引用都将被弱保存。

By convention the thing returned by calculateImageUsingAnotherProcess will be unowned other than by the autorelease pool. 按照惯例,除了自动释放池之外, calculateImageUsingAnotherProcess返回的内容将不归所有人所有。

So, although true, it's not so much that the strength of a reference "cannot be changed after the declaration", it's that there is no mechanism that allows you to attempt to do so and, correspondingly, no such attempt in your code. 因此,尽管是正确的,但并不仅仅是引用“在声明后不能更改”的强度,而是因为没有机制允许您尝试这样做,因此,您的代码也没有这样的尝试。

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

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