Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

fmquaglia
11,060 Pointsdot notation vs method call
Hello TreeHouse fellows,
In Building a Blog Reader Application, in the chapter Passing Data To The Detail View our instructor Amit taught us that (and I quote his words):
In case you are wondering why this is specified like a method,
and not using dot notation as we had seen previously,
you can interchange between dot notation and a method.
As per standard convention, if the property is read only
--which means you cannot write another value to it--
you should preferably call it as a method.
Please indulge my curiosity (I come from Java and Ruby land where dot notation is a de facto language construction).
Is there a technical reason to prefer the dot notation when a property is not read only?
Thanks!
3 Answers

John W
21,558 PointsOn dot notation: Dot notation in Objective-C is actually a relatively new thing and is very different from Java's dot notation. It is strictly a shorthand notation for calling the setter and getter methods, equivalent to Java's anObject.setVar(value)
and anObject.getVar()
. They are not direct access to variables. That's because ALL variables in Objective-C are private
. Dot notations simply get converted to the respective method calls for you. Hence, when you have
label.text = @"Hi";
You are really saying [label setText:@"Hi"]
. And if you are using the dot notation on the right hand side:
whatsInTheLabel = label.text;
You are really saying whatsInTheLabel = [label getText]
(In Objective-C, the getter would simply be [label text]
, but I gave you getText since you came from a Java background)
For readonly properties, only the getter, i.e. [label getText] is synthesized for you, so you can't use the dot notation on the left hand side as it will try to call the setter (i.e. [label setText:], which doesn't exist) for you.
For readwrite properties, both setter and getter are synthesized.

John W
21,558 PointsAbsolutely! some might disagree though, since the @property directive does provide automatic setter/getter synthesis so you don't have to code up something like -(id)getSomething{ return _something; }
for every variable like we did in Objective-C 1.0
Like in Java, you are always welcomed to override these methods and add codes so they get called when the variables are accessed. But the bottom-line is, now with 1 line of property declaration, you get the basic setter/getter for free.

fmquaglia
11,060 PointsSweet!
Thanks John W !

fmquaglia
11,060 PointsThanks for the clarification, John. Would be fair to say that in this case the dot notation is syntactic sugar only?