Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two reasons for having convenience constructor class methods. The first one is, that the idiom of <code>[[Thing alloc] initWithFoo: xyz]</code> is really common but inconvenient to have to type anywhere. So, <code>[Thing thingWithFoo: xzy]</code> is a common abbreviation.</p> <p>A deeper reason has to do with reference counting. Methods starting with <code>init</code> are supposed to return a reference of the instance, ownership of which is transferred to the caller. Wheras the convenience class methods usually return <code>autorelease</code>d references:</p> <pre><code>+ (id)packetWithType:(PacketType)packetType { return [[[[self class] alloc] initWithType:packetType] autorelease]; } </code></pre> <p>This is important to know in order to avoid dangling references and/or memory leaks:</p> <pre><code>Thing* thing = [[Thing alloc] initWithFoo: xyz]; // Now, *I* own the reference and *I* am responsible for releasing // it, when I no longer need it. [thing release] </code></pre> <p>On the other hand, the reference returned by</p> <pre><code>Thing* thing = [Thing thingWithFoo: xyz]; </code></pre> <p>is owned by the "nearest" <code>NSAutoreleasePool</code>. The caller is not responsible for releasing it (in fact, that would be wrong!). If the reference is to be kept around, the caller must actually <code>retain</code> it here:</p> <pre><code>self-&gt;myMember = [thing retain]; </code></pre> <p>You should know about these conventions even when using ARC, as the underlying rules are still in effect, even if (under ARC) it's the compiler, who generates the code to obey them. The <code>NARC</code> acronym is a nice way to remember, which method name prefixes come with certain responsibilities. <a href="https://stackoverflow.com/a/3672871/141081">This answer has the details</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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