Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using ARC, it <strong>will</strong> automatically dealloc <code>str</code> if it is pointing to a string with no other references and you set it to a new string object. You don't need to worry about managing the old string when you are setting a new value.</p> <p>ARC stands for Automatic Reference Counting and it takes care of calling <code>retain</code> and <code>release</code> for you at the appropriate times (such as when you change the value of <code>str</code>). In fact, you can't even manually call those functions anymore, but the same things is taking place "behind the scenes".</p> <p>As an aside, you say "I can't manually deallocate the NSString* str if not nil.". What you normally do to deallocate an object when using ARC is to just set all of the references to it to <code>nil</code>. So in this case, when you want to deallocate the string (assuming that <code>str</code> is your only reference to it) you simply need to set <code>str</code> to <code>nil</code>: <code>str = nil;</code>. Keep in mind however, as I said above, that you do not need to do this in order to set <code>str</code> to a new value. ARC is smart enough to take care of both at the same time.</p> <p>Another aside: You have the following line of code:</p> <pre><code>str = [[NSString alloc] initWithString:localStr]; </code></pre> <p>If you actually intended to make a copy, you can replace this with:</p> <pre><code>str = [localStr copy]; </code></pre> <p>It is a little shorter and easier to understand. More likely, you don't need an actual copy and you can just keep a reference to the existing string, so you can use:</p> <pre><code>str = localStr; </code></pre>
 

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