Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a lot of answers, but also a big confusion. I'll try to put some order (or increase the mess, we'll see...)</p> <ol> <li><p>Let's stop talking about Xcode. Xcode is an <strong>IDE</strong>. clang is a <strong>compiler</strong>. This feature we are discussing is called <em>autosynthesis of properties</em> and it's an <a href="http://clang.llvm.org/docs/LanguageExtensions.html#objective-c-autosynthesis-of-properties" rel="noreferrer">Objective-C language extension supported by clang</a>, which is the default compiler used by Xcode. <br> Just to make it clear, if you switch to gcc in Xcode, you won't benefit from this feature (regardless from the Xcode version.) In the same way if you use a text editor and compile using clang from the command line, you will.</p></li> <li><p>Thank to autosynthesis you don't need to explicitly synthesize the property as it will be automatically synthesized by the compiler as</p> <pre><code>@synthesize propertyName = _propertyName </code></pre> <p>However, a few exceptions exist:</p> <ul> <li><p><strong>readwrite property with custom getter and setter</strong></p> <p>when providing <strong>both</strong> a getter and setter custom implementation, the property won't be automatically synthesized</p></li> <li><p><strong>readonly property with custom getter</strong></p> <p>when providing a custom getter implementation for a readonly property, this won't be automatically synthesized </p></li> <li><p><strong>@dynamic</strong></p> <p>when using <code>@dynamic propertyName</code>, the property won't be automatically synthesized (pretty obvious, since <code>@dynamic</code> and <code>@synthesize</code> are mutually exclusive)</p></li> <li><p><strong>properties declared in a @protocol</strong></p> <p>when conforming to a protocol, any property the protocol defines won't be automatically synthesized</p></li> <li><p><strong>properties declared in a category</strong></p> <p>this is a case in which the <code>@synthesize</code> directive is not automatically inserted by the compiler, but this properties cannot be manually synthesized either. While categories can declare properties, they cannot be synthesized at all, since categories cannot create ivars. For the sake of completeness, I'll add that's it's still possible <a href="https://stackoverflow.com/questions/8733104/objective-c-property-in-category">to fake the property synthesis using the Objective-C runtime</a>.</p></li> <li><p><strong>overridden properties</strong> (new since clang-600.0.51, shipping with Xcode 6, thanks Marc Schlüpmann)</p> <p>when you override a property of a superclass, you must explicitly synthesize it</p></li> </ul></li> </ol> <p>It's worth noting that synthesizing a property automatically synthesize the backing ivar, so if the property synthesis is missing, the ivar will be missing too, unless explicitly declared.</p> <p>Except for the last three cases, the general philosophy is that whenever you manually specify all the information about a property (by implementing all the accessor methods or using <code>@dynamic</code>) the compiler will assume you want full control over the property and it will disable the autosynthesis on it.</p> <p>Apart from the cases that are listed above, the only other use of an explicit <code>@synthesize</code> would be to specify a different ivar name. However conventions are important, so my advice is to always use the default naming.</p>
 

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