A beginner, confused with dot and bracket notation

Discussion in 'iOS Programming' started by Vishwas Gagrani, Sep 26, 2012.

  1. Vishwas Gagrani, Sep 26, 2012
    Last edited by a moderator: Sep 26, 2012

    Vishwas Gagrani macrumors newbie

    Joined:
    Sep 26, 2012
    #1
    hi!
    I am quite confused, as the objective C code i see, is generally a mix of syntaxes that sometimes use dot notation and sometimes bracket notation.

    like

    Code:
    -(void)loadView 
    {
    
        self.view.backgroundColor = [UIColor whiteColor] ; 
    
       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
        [button setTitle:@"click" forState:UIControlStateNormal]; 
        [self.view addSubview:Button ] ;
    
    }

    I wonder why some of the line use dot notation, while some lines use bracket notation ?
    Can't everything be written either using dots or using brackets only ?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Don-notation is just syntactic-sugar (added in Objective-C 2.0) for using brackets with declared-properties. So:
    Code:
    object.propertyName = value;
    actually gets translated by the compiler to:
    Code:
    [object setPropertyName:value];
    and
    Code:
    variable = object.propertyName;
    actually gets translated by the compiler to:
    Code:
    variable = [object propertyName];
    No, on the first part. Yes, on the second.
     
  3. Vishwas Gagrani thread starter macrumors newbie

    Joined:
    Sep 26, 2012
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    Some traditional developers don't like dot notation because it obscures what's really going on.

    In C, dots are used to select fields within a struct:

    Code:
    typedef struct
    {
      GLfloat x;
      GLfloat y;
      GLfloat z;
    } GLPoint;
    
    
    GLPoint somePoint;
    

    Code:
    somePoint.x = 10;
    That code is not invoking a setter, it's addressing a field in a struct. So, when you read code using dot notation, you can't tell for sure which type of code you are looking at.

    I am an experienced Cocoa developer who likes dot notation.

    This:

    Code:
    [[self view] bounds] = newBounds;
    
    Is tedious to write, and I find it hard to parse. I have to look at it really closely to figure out the nesting of the brackets. It gets really bad if there are 3 or more levels of nesting. I much prefer

    Code:
    someObject.property.subProperty.subSubProperty = newValue;
     
  5. Vishwas Gagrani thread starter macrumors newbie

    Joined:
    Sep 26, 2012
    #5
    Thnx.

    Btw, while going through the code i notice the use as follows :

    1) [instanceName attribute:value] << Understandable

    2) [ ClassName attribute:value] << But, What is this ???
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    That is a message to a class rather than to an instance.

    There are instance methods, and class methods.

    An instance method is a method that responds to messages sent to an instance of a class (send a message to Bob's car telling it to start.)

    A class method is a method that responds to messages sent to the entire class, not an instance of a class.

    Instance methods start with a dash:

    Code:
    - (void) instanceMethod;
    
    And class methods start with a plus sign:

    Code:
    + (void) classMethod;
    Class methods are often "factory" methods that create an instance of that class and return it.

    For example, the NSString method stringWithFormat creates a string object and returns it:

    Code:
    + (id)stringWithFormat:(NSString *)format, ...
    
    (ignore the 3 dots at the end of the declaration for now. That's a tricky bit that lets that method handle variable numbers of parameters)

    Code:
    NSString *aString = [NSString stringWithFormat: @"The %@ shiek's %@ sheep's sick", @"sick", @"sixth"];
    
    That code sends a "stringWithFormat" message to the NSString class, and the NSString class uses it to create and return a new string object.
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    In many programming languages, there is far more than one legal way to write stuff that does exactly the same thing (compiles down to the same machine code). And for each way, there will be coders who argue that that way is better than the others.

    Best learn to deal with this.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    What the cool cats do is use dot notation when accessing anything declared with @property. This is for both getting and setting. And they use brackets for sending any other messages.

    There are a few exceptions to this but that's considered the best style.
     
  10. Vishwas Gagrani thread starter macrumors newbie

    Joined:
    Sep 26, 2012
    #10

    amm.. seems like something in between "Class Constructors" , and "Static Methods" of a class.
     
  11. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11
    If I remember my C++ terminology, it's a static method. It's a method that belongs to the class instead of an instance of the class.

    You can use Objective C class methods as class constructors or as static methods. Objective C doesn't have a formal definition of a class constructor.
     
  12. lasash macrumors member

    Joined:
    Sep 15, 2012
    #12
    The brackets was hard for me as well at first, but now I see the logic behind it.

    It's more neat to write with the brackets, because you know exactly what this line of code does.
     
  13. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #13
    This is a religious debate. There are passionate opinions on both sides.

    There are very senior developers who's opinion I respect who feel passionately that dot notation is stupid and should never have been created, and will never use it. I happen to disagree with them, but I respect their opinion nevertheless.

    I personally tend to get lost when there are more than 2 or 3 levels of nested brackets, and have to carefully match up the pairs of brackets in order to parse the sense of a statement. I've spent 5 minutes before trying to get a complex statement to compile when I've got a mismatched bracket somewhere. Sometimes my brain just "goes south" and can't see the problem.

    I find dot notation much easier to write and much easier to read.

    I almost never use C structures in Objective C, and so there isn't much chance of misreading code in dot notation.
     
  14. Vishwas Gagrani thread starter macrumors newbie

    Joined:
    Sep 26, 2012
    #14
    Ya, that makes sense. This is normally the case even with the nested if-else and for loop statements. Much time get wasted in wrong nesting, inspite the fact they can be marked on separate lines.
     

Share This Page