Objective-C dot notation?

Discussion in 'Mac Programming' started by manPrime, Apr 21, 2012.

  1. macrumors newbie

    Apr 21, 2012
    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
  2. macrumors 68000

    Mar 29, 2009
    Aaron's explaination from the book goes like so:
  3. macrumors 68020


    Feb 16, 2012
    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.
  4. thread starter macrumors newbie

    Apr 21, 2012
    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
  5. macrumors 68000

    Mar 29, 2009
    The iPhone programming book.

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

  6. macrumors 68000


    Aug 17, 2009
    There appears to be a tpyo in the last line, should it not read
    employee.caffeinationLevel = 100;
  7. macrumors 6502a

    Jan 23, 2010
    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.
  8. macrumors 68030


    Sep 13, 2001
    Portland, OR
    No. There is literally no difference in generated code between dot notation and bracket notation. Please stop spreading this misconception.
  9. macrumors 604

    Aug 9, 2009
    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":

    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).

    KVO uses willChangeValueForKey: and didChangeValueForKey:, not willSet and didSet.
  10. macrumors 604

    Aug 9, 2009
    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.

Share This Page