Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The most modern way<sup>1</sup>:</p> <ul> <li>whenever possible, declare properties </li> <li>don't declare iVars separately <sup>2</sup> </li> <li>don't @synthesize <sup>3</sup></li> <li>locate as few properties as possible in you .h file <sup>4</sup> </li> <li>locate as many properties as possible in a class extension in your .m file <sup>5</sup> </li> </ul> <hr> <p><sup>1</sup> As of Xcode 4.5.2. Most of this applies back to 4.4, some of it won't compile on 4.2 (the last version available under Snow Leopard). This is preprocessor stuff, so it is all compatible back at least to iOS5 (I haven't tested on iOS4 but that should also be OK).</p> <p><sup>2</sup> There is no point in declaring an iVar <em>as well as</em> a property. I am sure there are a few obscure cases where you would want to declare iVars <em>instead</em> of properties but I can't think of any. </p> <p><sup>3</sup> Xcode will create an iVar with the same name as the property, preceded by an _underscore. If you (rarely) need some other kind of behaviour, you can manually <code>@synthesize property = someOtherName</code>. @vikingosegundo links us to <a href="http://www.cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html">this article on dynamic ivars</a>, which is a use case for <code>@synthesize</code>. @RobNapier comments that you <em>do</em> need to <code>@synthesize iVar = _iVar</code> (bizarrely) if you are creating your own getters (readonly) and setters (read/write) for a property, as in this case the preprocessor will not generate the iVar for you.</p> <p><sup>4</sup> The general rule with your interface: keep it as empty as possible. You don't actually need to declare your <em>methods</em> now at all, if they are for private use. If you can get the code to work without an interface declaration, that's the way to go. </p> <p><sup>5</sup> This is an @interface block in your .m file, placed above your @implementation: </p> <pre><code>#TestClass.m @interface TestClass() //private property declarations here @end @implementation TestClass ... </code></pre>
 

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