Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As far as I know, <code>strong</code> and <code>retain</code> are synonyms, so they do <em>exactly</em> the same.</p> <p>Then the <code>weak</code> is almost like <code>assign</code>, but automatically set to nil after the object, it is pointing to, is deallocated.</p> <p><strong>That means, you can simply replace them.</strong></p> <p><strong>However</strong>, there is one special case I've encountered, where I had to use <code>assign</code>, rather than <code>weak</code>. Let's say we have two properties <code>delegateAssign</code> and <code>delegateWeak</code>. In both is stored our delegate, that is owning us by having the only strong reference. The delegate is deallocating, so our <code>-dealloc</code> method is called too.</p> <pre><code>// Our delegate is deallocating and there is no other strong ref. - (void)dealloc { [delegateWeak doSomething]; [delegateAssign doSomething]; } </code></pre> <p>The delegate is already in deallocation process, but still not fully deallocated. The problem is that <strong><code>weak</code> references to him are already nullified!</strong> Property <code>delegateWeak</code> contains nil, but <code>delegateAssign</code> contains valid object (with all properties already released and nullified, but still valid).</p> <pre><code>// Our delegate is deallocating and there is no other strong ref. - (void)dealloc { [delegateWeak doSomething]; // Does nothing, already nil. [delegateAssign doSomething]; // Successful call. } </code></pre> <p>It is quite special case, but it reveal us how those <code>weak</code> variables work and when they are nullified.</p>
 

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