Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Say you have a pointer <code>myView</code> defined in your class' interface:</p> <pre><code>@interface MyClass { UIView *myView; } @end </code></pre> <p>Then in your code, at some point, you may release that variable:</p> <pre><code>[myView release]; </code></pre> <p>After you do that, <code>myView</code>, the pointer, won't be pointing to nil, but will be pointing to a memory address of an object that may not exist anymore (since you just released it). So, if you happen to do something after this, like:</p> <pre><code>[myView addSubview:otherView]; </code></pre> <p>you'll get an error.</p> <p>If, on the other hand, you do this:</p> <pre><code>[myView release]; myView = nil; ... [myView addSubview:otherView]; </code></pre> <p>the call to <code>addSubview</code> will not have any negative impact, since <strong>messages to nil are ignored</strong>.</p> <p>As a corollary, you may see suggestions of using <code>retain</code> properties, such as:</p> <pre><code>@property(retain) UIView *myView; </code></pre> <p>and then in the code, just do:</p> <pre><code>self.myView = nil; </code></pre> <p>By doing that, the synthesized accessor will release the old object and set the reference to nil in one line of code. This may prove useful if you want to make sure all your properties are both released and set to nil.</p> <p>One thing that you must never forget, is that memory management is done by means of <code>retain</code> <code>release</code> calls, and not by means of assigning nil. If you have an object with a retain count of 1, and assign nil to it's sole variable, you'll be leaking memory:</p> <pre><code>UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,10,10)]; view = nil; // You just leaked a UIView instance!!! </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