PDA

View Full Version : Objective-C dot notation?




manPrime
Apr 21, 2012, 09:25 AM
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
Apr 21, 2012, 11:04 AM
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
Apr 21, 2012, 11:09 AM
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
Apr 21, 2012, 12:02 PM
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
Apr 21, 2012, 05:23 PM
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"

http://i.imgur.com/IXVAMl.jpg?1

Sydde
Apr 22, 2012, 08:42 PM
Here's another opinion from Robert Clair's book "Learning Objective-C 2.0"

Image (http://i.imgur.com/IXVAMl.jpg?1)
There appears to be a tpyo in the last line, should it not read employee.caffeinationLevel = 100;?

mfram
Apr 22, 2012, 11:09 PM
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
Apr 22, 2012, 11:55 PM
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
Apr 23, 2012, 12:02 AM
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/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html

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:

[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/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html
http://developer.apple.com/library/mac/#documentation/general/conceptual/devpedia-cocoacore/KeyValueCoding.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

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

chown33
Apr 23, 2012, 12:32 AM
There appears to be a tpyo in the last line, should it not read 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 (http://en.wikipedia.org/wiki/Homophone_Word_Game) of each.