Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't initialize <code>self</code> outside an <code>init</code> method because of the following <a href="https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/ObjectCreation.html#//apple_ref/doc/uid/TP40008195-CH39-SW1" rel="noreferrer">convention</a>:</p> <blockquote> <p>If an object’s class does not implement an initializer, the Objective-C runtime invokes the initializer of the nearest ancestor instead.</p> </blockquote> <p>and because the name of some methods is recognized by the memory management system of the runtime:</p> <blockquote> <p>When the name of a method starts with alloc init, retain, or copy, it means it's being created for the caller, who has the responsibility to release the object when he is done with it. Otherwise the method returned is not owned by the caller, and he has to indicate he wants to keep it calling retain on the object.</p> </blockquote> <p>Therefore, all initializers should be written as a variation of this:</p> <pre><code>- (id) init { self = [super init]; if (self){ _someVariable = @"someValue"; } return self; } </code></pre> <ul> <li>Don't use <code>if ((self = [super init]))</code> because it's ugly.</li> <li>Don't use <code>self.someVariable</code> because the object may not be initialized yet. Use direct variable access instead (<code>_someVariable</code>).</li> <li>We write <code>self = [super init]</code> and not just <code>[super init]</code> because a different instance may be returned.</li> <li>We write <code>if (self)</code> because there will be cases where it will return <code>nil</code>. </li> </ul> <p>However, there are two more problems with your method:</p> <ul> <li>You are combining object creation (<code>[super init]</code>) and an action (<code>-showDropDown::::</code>) in the same method. You should write two separate methods instead.</li> <li>The name of your method is <code>-showDropDown::::</code>. Objective-C programmers expect self documenting method names like <code>-showDropDown:height:array:direction:</code> instead. I guess you come from a different language, but <em>when in Rome, do as Romans do</em>, or else you won't be playing along with the rest of the team.</li> </ul>
 

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