Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>In Objective-C, it's possible to return an instance other than self from <code>-init</code>. Classes do this, for example, to enforce a singleton instance, or in the case of class clusters. <code>NSNumber</code>, for example, returns a subclass depending on the type of value passed to its initializer. So when you call <code>[[NSNumber alloc] initWithLong:long_value]</code>, <code>NSNumber</code>'s <code>-initWithLong:</code> initializer is called after <code>NSNumber</code>'s <code>+alloc</code>, but a subclass of <code>NSNumber</code> may be returned to the oringial caller. Thus the pattern</p> <p>self = [super init];</p> <p>which reassigns <code>self</code> to the value of <code>[super init]</code> so that <code>self</code> points to the actual instance that <code>[super init]</code> returned. If <code>+alloc</code> or the super's <code>init</code> method fails, the result of <code>[super init]</code> may be <code>nil</code>. To avoid, side effects in the case of a failed initialization, the pattern then becomes</p> <blockquote> <pre><code>- (id) init { if(self = [super init]) { // do initialization of instance variables etc. } return self; } </code></pre> </blockquote> <p>Note that you must return <code>self</code> (or <code>nil</code> or an other instance) from the <code>init</code> method. You <em>should</em> assign self to <code>[super init]</code> and you may check for <code>nil</code> before doing more work.</p></li> <li><p>You may have to release the return value of a staic method. You should read the Cocoa memory management <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html" rel="nofollow noreferrer">guide</a>. The rule is generally quite simple: If the method you call has "new", "alloc", or "copy" in its signature, the result belongs to the caller and the caller must call <code>-release</code> on that instance or there will be a memory leak. Of course you should call <code>-retain</code> on anything else (i.e. not from an "alloc","new" or "copy" method) you want to keep a reference to and then call <code>-release</code> or <code>-autorelease</code> when you are done with that instance.</p></li> <li><p><code>str = @"Hi there!"</code>, assuming <code>str</code> was declared as <code>NSString *str;</code> assigns the address of the string constant <code>@"Hi there!" to the value of the</code>str<code>variable. You do not need to retain or release string constants.</code>str = [[NSString alloc] initWithString:@"Hi there!"];<code>allocates a new string instance. The value of</code>str<code>will be the address of this instance. Each call of</code>str = [[NSString alloc] initWithString:@"Hi there!"];<code>again will allocate a new instance. So after</code>str2 = [[NSString alloc] initWithString:@"Hi there!"];<code>,</code>str != str2<code>, while after</code>str2 = @"Hi There!", <code>str==str2</code>. See <a href="https://stackoverflow.com/questions/706519/is-an-nsmutablestring-in-this-case-more-efficient-than-nsstring/706610#706610">this</a> answer as well.</p></li> <li><p><code>-autorelease</code> adds the receiver to the current <code>NSAutoreleasPool</code>. When the pool is drained (usually at the end of the current run loop iteration, or when the pool is manually drained), the pool calls <code>-release</code> on all instances in the pool. If this <code>-release</code> drops the retain count to 0, the object is deallocated (and <code>-dealloc</code> called) just as with any other <code>-release</code>. Using an autorelease pool is generally frowned upon on the iPhone because it may cause you to accumulate many unused instances in the pool before it is drained at the end of the run loop iteration. If you can use <code>-release</code> instead of <code>-autorelease</code>, you generally should. Again, see the Cocoa memory management <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html" rel="nofollow noreferrer">guide</a> for more info. </p></li> </ol>
 

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