Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

manPrime

macrumors newbie
Original poster
Apr 21, 2012
21
0
Hi i'm currently attempting to learn Objective-C through the use of both Stephen Kochan and Aaron Hillegass books. However i have a question about dot notation. In Stephen's books he uses dot notation frequently where as in Aaron's book he advises that you don't use it. So my question is, is dot notation acceptable? do you real programmers use it in the real world? i mean even Apple uses it in its documentation!

Thanks manPrime
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
Aaron's explaination from the book goes like so:
The argument for using dot-notation is that the dot signifies that the code is accessing the state of an object whereas using the brackets signifies that it is asking the object to perform some behavior. This supposedly gives the code clarity. The arguments against dot-notation are that it creates ambiguity with the C structure access operator and it confuses beginning programmers, especially when it comes to memory management.
This book will not use dot-notation because it is confusing to beginning programmers. If you choose to use dot-notation after you’ve mastered the concepts behind Objective-C, more power to you. For now, you will be better served by sticking with the brackets.
 

charlieegan3

macrumors 68020
Feb 16, 2012
2,394
17
U.K
i wouldn't get to het up about it, if your just starting out go with what you find most natural and stick with that until you find reason to stop.
 

manPrime

macrumors newbie
Original poster
Apr 21, 2012
21
0
Thanks for your replies. Which book is that from? or is that him explaining his decision in the book. Anyway i haven't been using it so i guess i'l stick with that :D
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
Thanks for your replies. Which book is that from? or is that him explaining his decision in the book. Anyway i haven't been using it so i guess i'l stick with that :D

The iPhone programming book.

Here's another opinion from Robert Clair's book "Learning Objective-C 2.0"

IXVAMl.jpg
 

mfram

Contributor
Jan 23, 2010
1,311
352
San Diego, CA USA
One reason to use the dot notation is that I believe the compiler is doing a lot of work for you. I think it handles multi-threaded synchronization as well as the calling the accessor functions like willSetBlah: and didSetBlah:, etc. I would use the dot notation wherever possible.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
One reason to use the dot notation is that I believe the compiler is doing a lot of work for you. I think it handles multi-threaded synchronization as well as the calling the accessor functions like willSetBlah: and didSetBlah:, etc. I would use the dot notation wherever possible.

No. There is literally no difference in generated code between dot notation and bracket notation. Please stop spreading this misconception.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,755
8,445
A sea of green
One reason to use the dot notation is that I believe the compiler is doing a lot of work for you. I think it handles multi-threaded synchronization as well as the calling the accessor functions like willSetBlah: and didSetBlah:, etc. I would use the dot notation wherever possible.
The dot notation is simply a shorthand that calls a method. If the method is atomic, so is the setter and getter. If the method isn't atomic, then neither are the setter and getter.

See "Dot Syntax":
http://developer.apple.com/library/...ual/objectivec/Chapters/ocObjectsClasses.html

Code:
myInstance.value = 10;
printf("myInstance value: %d", myInstance.value);
When used with objects, however, dot syntax acts as “syntactic sugar”—it is transformed by the compiler into an invocation of an accessor method. Dot syntax does not directly get or set an instance variable. The code example above is exactly equivalent to the following:

Code:
[myInstance setValue:10];
printf("myInstance value: %d", [myInstance value]);
[underline added for emphasis]
Since the two things are exactly equivalent, there is neither more nor less happening with dot notation, as compared to invoking the accessor method. They are exactly equivalent. Not approximately, exactly.


Any willSetBlah: and didSetBlah: is not part of the dot notation. Perhaps you're thinking of KVC (key-value coding) or key-value observing (KVO).
https://developer.apple.com/library...l/KeyValueCoding/Articles/KeyValueCoding.html
http://developer.apple.com/library/...eptual/devpedia-cocoacore/KeyValueCoding.html
http://developer.apple.com/library/...tual/KeyValueObserving/KeyValueObserving.html

KVO uses willChangeValueForKey: and didChangeValueForKey:, not willSet and didSet.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,755
8,445
A sea of green
There appears to be a tpyo in the last line, should it not read
Code:
employee.caffeinationLevel = 100;
?

Your write[1]. It shuold.

This is one reason to always check the website associated with any programming book. A list of printing errors can be crucial. In olden days, I remember a widely circulated file, "The Errata of Computer Programming", compiled by Knuth. It was eye-opening to see how much even the most careful professionals get wrong.


[1] Two of my favorite words, for the number of homophones of each.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.