iOS A beginner, confused with dot and bracket notation

Vishwas Gagrani

macrumors newbie
Original poster
Sep 26, 2012
21
0
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 ?
 
Last edited by a moderator:

dejo

Moderator
Staff member
Sep 2, 2004
15,725
447
The Centennial State
I wonder why some of the line use dot notation, while some lines use bracket notation ?
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];
Can't everything be written either using dots or using brackets only ?
No, on the first part. Yes, on the second.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Thanks, that was helpful.
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;
 

Vishwas Gagrani

macrumors newbie
Original poster
Sep 26, 2012
21
0
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 ???
 

chown33

Moderator
Staff member
Aug 9, 2009
8,427
4,432
Pale blue comma

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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 ???
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.
 

firewood

macrumors 604
Jul 29, 2003
7,645
872
Silicon Valley
I wonder why some of the line use dot notation, while some lines use bracket notation ?
?
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

Vishwas Gagrani

macrumors newbie
Original poster
Sep 26, 2012
21
0
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.

amm.. seems like something in between "Class Constructors" , and "Static Methods" of a class.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
amm.. seems like something in between "Class Constructors" , and "Static Methods" of a class.
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.
 

lasash

Suspended
Sep 15, 2012
36
0
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.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.
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.
 

Vishwas Gagrani

macrumors newbie
Original poster
Sep 26, 2012
21
0
I personally tend to get lost when there are more than 2 or 3 levels of nested brackets,
.
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.