简体   繁体   中英

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.

OK, in the first case image is weak but what about the second? Will image maintain the weak aspect?

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.

By convention the thing returned by calculateImageUsingAnotherProcess will be unowned other than by the autorelease pool.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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