Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Under the hood, Objective-C objects are basically C structs. Each one contains a field called <code>isa</code>, which is a pointer to the class that the object is an instance of (that's how the object and Objective-C runtime knows what kind of object it is).</p> <p>Regarding the initialization of variables: in Objective-C, instance variables are automatically initialized to <code>0</code> (for C types like <code>int</code>) or <code>nil</code> (for Objective-C objects). Apple's guidelines say that initializing your ivars to those values in your <code>init</code> methods is redundant, so don't do it. For example, say you had a class like this:</p> <pre><code>@interface MyClass : NSObject { int myInt; double myDouble; MyOtherClass *myObj; } @end </code></pre> <p>Writing your <code>init</code> method this way would be redundant, since those ivars will be initialized to <code>0</code> or <code>nil</code> anyway:</p> <pre><code>@implementation MyClass - (id)init { if ((self = [super init])) { myInt = 0; myDouble = 0.0; myObj = nil; } return self; } @end </code></pre> <p>You can do this instead:</p> <pre><code>@implementation MyClass - (id)init { return [super init]; } @end </code></pre> <p>Of course, if you want the ivars to be initialized to values other than <code>0</code> or <code>nil</code>, you should still initialize them:</p> <pre><code>@implementation MyClass - (id)init { if ((self = [super init])) { myInit = 10; myDouble = 100.0; myObj = [[MyOtherClass alloc] init]; } return self; } @end </code></pre>
    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. 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.
 

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