<?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: Objective-C: Properties, Setters and Dot Syntax</title>
	<atom:link href="http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/feed" rel="self" type="application/rss+xml" />
	<link>http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html</link>
	<description>Tips, tools and code for iPhone and Mac developers.</description>
	<lastBuildDate>Tue, 01 May 2012 00:21:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: john</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/comment-page-1#comment-442</link>
		<dc:creator>john</dc:creator>
		<pubDate>Sun, 17 Aug 2008 01:40:00 +0000</pubDate>
		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=227#comment-442</guid>
		<description>Comments are closed on this post. You can comment on this same post which has been moved to the iPhone Developer Tips blog: http://iphonedevelopertips.com/objective-c/properties-setters-and-dot-syntax.html</description>
		<content:encoded><![CDATA[<p>Comments are closed on this post. You can comment on this same post which has been moved to the iPhone Developer Tips blog: <a href="http://iphonedevelopertips.com/objective-c/properties-setters-and-dot-syntax.html" rel="nofollow">http://iphonedevelopertips.com/objective-c/properties-setters-and-dot-syntax.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: john</title>
		<link>http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/comment-page-1#comment-402</link>
		<dc:creator>john</dc:creator>
		<pubDate>Thu, 24 Jul 2008 23:04:31 +0000</pubDate>
		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=227#comment-402</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://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/comment-page-1#comment-401</link>
		<dc:creator>stevebert</dc:creator>
		<pubDate>Thu, 24 Jul 2008 17:29:34 +0000</pubDate>
		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=227#comment-401</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://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/comment-page-1#comment-400</link>
		<dc:creator>john</dc:creator>
		<pubDate>Thu, 24 Jul 2008 12:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=227#comment-400</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://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html/comment-page-1#comment-399</link>
		<dc:creator>Bertg</dc:creator>
		<pubDate>Thu, 24 Jul 2008 07:48:09 +0000</pubDate>
		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=227#comment-399</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>

