Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the second part of the question, property definition is not needed, it is a help to us . The @synthesize directive on property generates accessor methods for properties so we don't have to do it manually, and because:</p> <blockquote> <p>This code instructs the compiler to generate, or synthesize, the accessor methods. The compiler will generate the accessor methods using well-tested, fast algorithms that are ready for multi-core and multi-threaded environments, including locking variables in setter methods. Not only does using properties reduce the amount of code that you have to write, it replaces that code with the best possible accessors for today's modern multi-core systems. Later, if you need to provide an alternative implementation for a property accessor, you can simply add the appropriate code to your class.</p> <p><a href="http://developer.apple.com/leopard/overview/objectivec2.html" rel="nofollow noreferrer">http://developer.apple.com/leopard/overview/objectivec2.html</a></p> </blockquote> <p>The <code>nonatomic</code> will avoid use of locking when accessing variables, if you don't specify anything then default is <code>atomic</code>. Locking is useful on multithreaded systems. The copy specifies what code should be generated for accessors, <code>copy</code> will copy the object, <code>retain</code> will retain new object and release old one, <code>assign</code> is good for simple variables like int to just plain assign values. So when you define your property as you did above <code>(nonatomic,copy)</code> and then use <code>self.MyProperty = @"Hey"</code> you're actually calling generated accessor that will make a copy of the new variable as opposed to just assigning it. You can override accessor and add checking to it.</p> <p><strong>Because of the above I would say that defining property has benefits even when the variable is not used outside of the class.</strong></p> <p>I believe to access properties you should use <code>self.MyProperty</code> instead of just <code>MyProperty</code> but I can't point you to explanation why. Might be something to do with the fact that compiler will generate from</p> <pre><code>self.MyProperty = @"Hey"; </code></pre> <p>this:</p> <pre><code>[self setMyProperty: @"Hey"]; </code></pre> <p>But I'm only speculating here.</p> <p>Whether you call self.MyProperty or MyProperty it should not affect memory management (I would still prefer the first - self.MyProperty).</p> <p>See <a href="http://developer.apple.com/leopard/overview/objectivec2.html" rel="nofollow noreferrer">Objective-C 2.0 Overview</a> for some high level description from Apple.</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