Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, read @DrummerB's answer. It a good overview of the whys and what you should generally do. With that in mind, to your specific questions:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; // 1) What do I declare here? </code></pre> <p>No actual variable definitions go here (it's technically legal to do so if you know exactly what you're doing, but never do this). You may define several other kinds of things:</p> <ul> <li>typdefs</li> <li>enums</li> <li>externs</li> </ul> <p>Externs look like variable declarations, but they're just a promise to actually declare it somewhere else. In ObjC, they should only be used to declare constants, and generally only string constants. For instance:</p> <pre><code>extern NSString * const MYSomethingHappenedNotification; </code></pre> <p>You would then in your <code>.m</code> file declare the actual constant:</p> <pre><code>NSString * const MYSomethingHappenedNotification = @"MYSomethingHappenedNotification"; </code></pre> <hr> <pre><code>@interface SampleClass : NSObject { // 2) ivar declarations // Pretty much never used? } </code></pre> <p>As noted by DrummerB, this is legacy. Don't put anything here.</p> <hr> <pre><code>// 3) class-specific method / property declarations @end </code></pre> <p>Yep.</p> <hr> <pre><code>#import "SampleClass.h" // 4) what goes here? </code></pre> <p>External constants, as described above. Also file static variables can go here. These are the equivalent of class variables in other languages.</p> <hr> <pre><code>@interface SampleClass() // 5) private interface, can define private methods and properties here @end </code></pre> <p>Yep</p> <hr> <pre><code>@implementation SampleClass { // 6) define ivars } </code></pre> <p>But very rarely. Almost always you should allow clang (Xcode) to create the variables for you. The exceptions are usually around non-ObjC ivars (like Core Foundation objects, and especially C++ objects if this is an ObjC++ class), or ivars that have weird storage semantics (like ivars that don't match with a property for some reason).</p> <hr> <pre><code>// 7) define methods and synthesize properties from both public and private // interfaces </code></pre> <p>Generally you shouldn't @synthesize anymore. Clang (Xcode) will do it for you, and you should let it.</p> <p>Over the last few years, things have gotten dramatically simpler. The side-effect is that there are now three different eras (Fragile ABI, Non-fragile ABI, Non-fragile ABI + auto-syntheisze). So when you see the older code, it can be a little confusing. Thus confusion arising from simplicity :D</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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