<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Properties, Setters and Dot Syntax</title>
	<atom:link href="http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 06:31:16 -0600</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Markus Gasser</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-2353</link>
		<dc:creator>Markus Gasser</dc:creator>
		<pubDate>Sun, 24 May 2009 19:58:49 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-2353</guid>
		<description>I know I&#039;m a bit late but maybe this still helps someone.

The reason the value is not retained is because self.m_database = foo; is using the property setter and will thus be compiled same as when you would call [self setM_database:foo];. Whereas when you use only m_database = foo; it is equivalent to self-&gt;m_database (notice the -&gt; instead of the dot) which is a direct access to your instance variable. This way you can get around sending a message (which is quite expensive) when all you want is to assign your instance variable, but then you have to keep in mind that you must retain / copy and check the new value yourself if needed.</description>
		<content:encoded><![CDATA[<p>I know I&#8217;m a bit late but maybe this still helps someone.</p>
<p>The reason the value is not retained is because self.m_database = foo; is using the property setter and will thus be compiled same as when you would call [self setM_database:foo];. Whereas when you use only m_database = foo; it is equivalent to self->m_database (notice the -> instead of the dot) which is a direct access to your instance variable. This way you can get around sending a message (which is quite expensive) when all you want is to assign your instance variable, but then you have to keep in mind that you must retain / copy and check the new value yourself if needed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nicholson</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-547</link>
		<dc:creator>Robert Nicholson</dc:creator>
		<pubDate>Mon, 06 Apr 2009 03:14:37 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-547</guid>
		<description>But more importantly the reason it works the way it does is that there has to be a way to bypass the property&#039;s accessors and that way is to not specify the receiver ie. self</description>
		<content:encoded><![CDATA[<p>But more importantly the reason it works the way it does is that there has to be a way to bypass the property&#8217;s accessors and that way is to not specify the receiver ie. self</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: honk</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-129</link>
		<dc:creator>honk</dc:creator>
		<pubDate>Thu, 11 Dec 2008 20:31:03 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-129</guid>
		<description>I&#039;ve been wondering one thing (I&#039;m a newbie in objective-c)

I have this function in my code

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.m_database = [Database new];
    MainViewController* viewController = [MainViewController alloc];
    [viewController initWithNibName:@&quot;MainView&quot; bundle:nil];
    self.m_mainViewController = viewController;
    [viewController release];
    m_infoButton.bounds = CGRectMake(-25, -25, 60, 50);
	
    [self.view insertSubview:m_mainViewController.view belowSubview:m_infoButton];
    [viewController setDatabase:m_database];
}

It works fine but there is one thing I don&#039;t understand... I can still write without &quot;self&quot; on the member variables but what happens is that the retain-attribute on my @property won&#039;t apply. This is interesting to point out that &quot;self.m_database =&quot; is not the same as &quot;m_database =&quot; even though the synthesize is called m_database. This apparently means that the setter-accessor is only used when &quot;self.&quot; is written in front of the variable

Or have I missed something?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been wondering one thing (I&#8217;m a newbie in objective-c)</p>
<p>I have this function in my code</p>
<p>- (void)viewDidLoad {</p>
<p>    [super viewDidLoad];</p>
<p>    self.m_database = [Database new];<br />
    MainViewController* viewController = [MainViewController alloc];<br />
    [viewController initWithNibName:@"MainView" bundle:nil];<br />
    self.m_mainViewController = viewController;<br />
    [viewController release];<br />
    m_infoButton.bounds = CGRectMake(-25, -25, 60, 50);</p>
<p>    [self.view insertSubview:m_mainViewController.view belowSubview:m_infoButton];<br />
    [viewController setDatabase:m_database];<br />
}</p>
<p>It works fine but there is one thing I don&#8217;t understand&#8230; I can still write without &#8220;self&#8221; on the member variables but what happens is that the retain-attribute on my @property won&#8217;t apply. This is interesting to point out that &#8220;self.m_database =&#8221; is not the same as &#8220;m_database =&#8221; even though the synthesize is called m_database. This apparently means that the setter-accessor is only used when &#8220;self.&#8221; is written in front of the variable</p>
<p>Or have I missed something?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: john</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-30</link>
		<dc:creator>john</dc:creator>
		<pubDate>Sun, 17 Aug 2008 01:21:12 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-30</guid>
		<description>It&#039;s also worthwhile to point out that using the dot syntax does not skirt around the setter. In other words, this code:

  SomeClass *ptr = [[SomeClass alloc] initWithStrAndDate:@&quot;Foo&quot;];
  ...
  ptr.str = @&quot;Foo 2&quot;;
  
looks like a direct assignment of the string &quot;Foo 2&quot; to the instance variable &#039;str&#039;. However, the work is still done through the setter. This is worth noting given that using properties and dot syntax result in a call to the setter, which is not as obvious as this [ptr setStr:@&quot;Foo 2&quot;] (which makes it clear the setter is involved).</description>
		<content:encoded><![CDATA[<p>It&#8217;s also worthwhile to point out that using the dot syntax does not skirt around the setter. In other words, this code:</p>
<p>  SomeClass *ptr = [[SomeClass alloc] initWithStrAndDate:@&#8221;Foo&#8221;];<br />
  &#8230;<br />
  ptr.str = @&#8221;Foo 2&#8243;;</p>
<p>looks like a direct assignment of the string &#8220;Foo 2&#8243; to the instance variable &#8216;str&#8217;. However, the work is still done through the setter. This is worth noting given that using properties and dot syntax result in a call to the setter, which is not as obvious as this [ptr setStr:@"Foo 2"] (which makes it clear the setter is involved).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stevebert</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-29</link>
		<dc:creator>stevebert</dc:creator>
		<pubDate>Thu, 14 Aug 2008 16:20:23 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-29</guid>
		<description>One thing to remember is the dot syntax based on how the @synthesize directive is declared.

@interface Foo : NSObject {
   NSString *str;
}
@property(assign, readwrite) NSString *str;
@end

@implementation Foo
@synthesize name = str;
- (void) test {
    self.str = @&quot;hi&quot;; // direct assignment to variable
    self.name = @&quot;hi&quot;; // assignment through setter
    [self setName: @&quot;hi&quot;];  // assignment through setter
}

The @synthesize directive generates the getter and setter functions, but also declare the name of the variable for the dot syntax.  Thus &quot;name&quot; is an alias for the getter/setter even though the member variable is &quot;str&quot;.  This becomes more important if you use the @property(retain,...) attribute:
@property (retain,readwrite) NSString *str;
...
    self.str = @&quot;hi&quot;; // direct assignment to variable
    self.name = @&quot;hi&quot;; // retained through setter
So while @synthesize does generate getter/setters using the Key-Value Coding rules, the dot syntax maps directly to the @synthesize name.  I find this a nice enhancement to the language.</description>
		<content:encoded><![CDATA[<p>One thing to remember is the dot syntax based on how the @synthesize directive is declared.</p>
<p>@interface Foo : NSObject {<br />
   NSString *str;<br />
}<br />
@property(assign, readwrite) NSString *str;<br />
@end</p>
<p>@implementation Foo<br />
@synthesize name = str;<br />
- (void) test {<br />
    self.str = @&#8221;hi&#8221;; // direct assignment to variable<br />
    self.name = @&#8221;hi&#8221;; // assignment through setter<br />
    [self setName: @"hi"];  // assignment through setter<br />
}</p>
<p>The @synthesize directive generates the getter and setter functions, but also declare the name of the variable for the dot syntax.  Thus &#8220;name&#8221; is an alias for the getter/setter even though the member variable is &#8220;str&#8221;.  This becomes more important if you use the @property(retain,&#8230;) attribute:<br />
@property (retain,readwrite) NSString *str;<br />
&#8230;<br />
    self.str = @&#8221;hi&#8221;; // direct assignment to variable<br />
    self.name = @&#8221;hi&#8221;; // retained through setter<br />
So while @synthesize does generate getter/setters using the Key-Value Coding rules, the dot syntax maps directly to the @synthesize name.  I find this a nice enhancement to the language.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: john</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-28</link>
		<dc:creator>john</dc:creator>
		<pubDate>Thu, 14 Aug 2008 15:01:47 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-28</guid>
		<description>I don&#039;t disagree. My comment is more about how getters and setters work in Objective-C when not using properties and how moving to using properties requires some rewiring on how you think about accessors. 

When *not* using properties, a best practice is that getter methods are simply the name of the instance variable (i.e. name versus getName), whereas a setter method pre-pends &#039;set&#039; to the instance variable name (i.e. setName). Therefore, calling the getter looks like this [someObject name] and calling the setter [someObject setName:@&quot;foo&quot;]. 

However, when using properties and dot syntax, the practice of pre-pending &#039;set&#039; to the instance variable names no longer applies. 

I can understand why the implementation works as it does (and your comment as well). If nothing else I point this out for those who are new to Objective-C, as I am. To understand how getters/setters work (and appreciate the value-add of properties), I started by writing my own accessors. Once making the move to use properties, it simply takes a little getting used to when you are expecting setters to have the format &#039;setInstanceVar&#039;.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t disagree. My comment is more about how getters and setters work in Objective-C when not using properties and how moving to using properties requires some rewiring on how you think about accessors. </p>
<p>When *not* using properties, a best practice is that getter methods are simply the name of the instance variable (i.e. name versus getName), whereas a setter method pre-pends &#8216;set&#8217; to the instance variable name (i.e. setName). Therefore, calling the getter looks like this [someObject name] and calling the setter [someObject setName:@"foo"]. </p>
<p>However, when using properties and dot syntax, the practice of pre-pending &#8216;set&#8217; to the instance variable names no longer applies. </p>
<p>I can understand why the implementation works as it does (and your comment as well). If nothing else I point this out for those who are new to Objective-C, as I am. To understand how getters/setters work (and appreciate the value-add of properties), I started by writing my own accessors. Once making the move to use properties, it simply takes a little getting used to when you are expecting setters to have the format &#8216;setInstanceVar&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bertg</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/properties-setters-and-dot-syntax.html#comment-27</link>
		<dc:creator>bertg</dc:creator>
		<pubDate>Thu, 14 Aug 2008 14:18:58 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=62#comment-27</guid>
		<description>Actually this way of setting seems more obvious. It basically mimics the setting of method variables.

foo = @&quot;Foo&quot;;
bar.foo = @&quot;Foo&quot;;

You could say that you &quot;scope&quot; your setting to the &quot;thing&quot; on the left side of the dot.

I&#039;ve been using many languages (java, c#, ruby) and just started learning Objective C. To me this way of setting and getting methods seems more natural.</description>
		<content:encoded><![CDATA[<p>Actually this way of setting seems more obvious. It basically mimics the setting of method variables.</p>
<p>foo = @&#8221;Foo&#8221;;<br />
bar.foo = @&#8221;Foo&#8221;;</p>
<p>You could say that you &#8220;scope&#8221; your setting to the &#8220;thing&#8221; on the left side of the dot.</p>
<p>I&#8217;ve been using many languages (java, c#, ruby) and just started learning Objective C. To me this way of setting and getting methods seems more natural.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

