Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

substituting for __weak when not using ARC

I have this line of code:

__weak NSBlockOperation *weakOperation = operation;

which is triggering this compiler error:

__weak attribute cannot be specified on automatic variable.

Reason for this is I don't have ARC enabled (not ready to make the switch just yet). So from another StackOverFlow question, I was recommended to use:

__unsafe_unretained NSBlockOperation *weakOperation = operation; 

Which makes the error go away, but for the context I'm using it, it's not working (see this question if interested: How to cancel NSOperationQueue).

So my question is, what I can substitute the __weak keyword with in this instance to get rid of this warning? Everything actually works correctly when I use __weak, but I'm afraid it won't hold up over future versions of iOS.

like image 452
Ser Pounce Avatar asked Jan 23 '13 16:01

Ser Pounce


1 Answers

You should not be worried about future versions of iOS because __weak is something interpreted by the compiler while producing code for you.

Looking at your other post suggests to me that your goal is to avoid weakOperation to be retained despite reference from within the block. In your specific case, where you don't use ARC, you can replace __weak by __block to indicate that your variable should not be retained during capture.

Note that the influence of__block on retain behavior is different between ARC and manual retain counting.

like image 195
s.bandara Avatar answered Nov 15 '22 07:11

s.bandara