Brackets vs dot, I am completely confused!


Let me write down the code from the book, for me to explain what I am missing:

    CGRect bounds = self.bounds;
    //Figure out the center of the bounds rectangle
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    //The circle will be the largest that will fit in the view
    float radius = (MIN(bounds.size.width, bounds.size.height) /2.0);
    UIBezierPath *path = [[UIBezierPath alloc] init];
    //Add an arc to the path at the center, with radius of radius, from 0 to 2*PI radians (a circle)
    [path addArcWithCenter:center
                  endAngle:M_PI * 2.0
    //Draw the line!
    [path stroke];

I thought dot and brackets notation were the same. So I tried to put the bracket notation in the center.x and center.y part of the code, but no way I could succeed. How would you do ?
[EDIT EVEN BEFORE I PUBLISH] I just noticed it’s only a C structure, so there is no bracket problem there, because there will never be any brackets! OK, one item fixed.

Then for the [path stroke] part, it actually says "to the class instance “path”, apply the instance method “stroke”, right ? so in dot notation, it would be path.stroke.
If I write down this in XCode, it says “Property access result unused - getters should not be used for side effects”.

??? What did I do wrong ? What is the damned difference between brackets and dot notation ? Which side effects ?

Thanks for your further answers !


There is a difference because the Objective-C language is a super set of the C language.

Therefore, you have to know what kind of object you are applying the dot operator or method-invocation operator to.

You can apply only the dot operator to objects constructed from C structs to access the value of a field in the struct.

When it comes to accessing a property of an Objective-C object, however, there is no difference between the effects of a dot operator and the method-invocation operator. Also you can’t use the dot operator to invoke a method!

Don’t always believe what you hear :slight_smile:


Thanks ibex10 for your answers.

So actually, it is not all black or white: sometimes we better use dot notation (or dot operator), sometimes the bracket notation (or method-invocation operator). Sometimes we have the choice, sometimes we don’t have.

For example:

  • C structs and C in general: only dot operator.
  • Access methods in Objective-C: we can use both. In " Objective-C Programing: the BNR Guide", Aaron prefers to use brackets (chapter 17), whereas in “iOS Programming: the BNR Guide 4th edition”, he and Joe prefer to use dot notation (p47). So if we take the most recent book, let’s say we’d rather use the dot operator.
  • Method-invocation in Objective-C: only brackets notation.

Is that right ? Is that all ?

Thanks !


If I may move on to the next chapter here: I wish to know if the expressions I quote down there are equivalent.

self.colorCircle = [UIColor lightGrayColor];
[self setCircleColor:[UIColor lightGrayColor]];
_circleColor = [UIColor lightGrayColor];
... more options ?
[self.circleColor setStroke];
[[self circleColor] setStroke];
[_circleColor setStroke];
... more options ?