Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Bill's answer isn't quite correct:</p> <p>If you have an instance of <code>A</code> and create a block inside of that instance like so:</p> <pre><code>^{ b = YES; } </code></pre> <p>Then <code>self</code> is retained (when the block is copied). <code>b</code> is <em>not</em> <code>const</code>-copied, because <code>b</code> is strongly referenced by <code>self</code>, and only <code>self</code> is <code>const</code> within the scope of the block.</p> <p>On the other hand, if you do:</p> <pre><code>BOOL aBool = YES; ^{ aBool = NO; c = [[NSObject alloc] init]; } </code></pre> <p>Then again, <code>self</code> is <code>const</code>-copied (and retained when the block itself is copied) and the assignment to <code>c</code> is allowed. However, the assignment to <code>aBOOL</code> is <em>not</em> allowed, because the value of <code>aBool</code> is <code>const</code>-copied.</p> <p>In other words, the compiler recognizes the <code>b</code> and <code>c</code> are ivars, and will retain <code>self</code> instead of the ivars directly.</p> <hr> <p>One way to think about this that helps me understand what's going on is to remember that an object is really just a fancy struct, which means you can technically access ivars via the arrow operator: <code>-&gt;</code></p> <p>So when you're accessing ivars:</p> <pre><code>b = YES; </code></pre> <p>is <em>equivalent</em> to:</p> <pre><code>self-&gt;b = YES; </code></pre> <p>In that light, it makes perfect sense <em>why</em> you have to retain <code>self</code>, but <code>b</code> is not <code>const</code>. It's because <code>b</code> is only a slim part of the "bigger picture", and in order to get <code>b</code>, you must necessarily include all of <code>self</code> as well (since copying part of a struct doesn't really make sense in this context).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload