Adam:
As you already know, square brackets are used to send a message to an object, so if a object like myObject implements the method -doSomething, you can call it with:
\[myObject doSomething\];
The other use square for brackets have is to index C-style arrays, such as:
aValue = anArray\[10\]; // Get the 10-element of an array
However, because C-style arrays are rarely used in Cocoa applications (use NSArray instead) you will not see this situation often.
Parenthesis in expressions are used to group and prioritize, such as:
x = 10 * (3 + 5); // x = 80
If () statements use it to delimit the test expression. Same with while (). for () uses it to enclose the limits and increment statements, etc.
The other important use of parenthesis is in calling a C function:
result = foo(3);
means that you are calling the function named foo with the argument 3, and storing the the return value in the variable 'result'. This is different from a message because a C function is not a method of an object. They exist independently of any objects. NSLog() is a function defined within Cocoa but it is not a method. So, you call it with the traditional C function call syntax. Cocoa defines other functions like NSStringFromRect(), which takes an NSRect as argument and returns a pointer to an NSString.
And that leads me to your last question. Some methods return simple types, like int, float, or void (i.e. returns no value). These methods will have prototypes like:
\- \(void\)returnNothing;
\- (int)returnInteger;
Other methods return pointers to types, like:
\- (int \*)returnPointerToInteger;
\- (void \*)returnPointerToAnything; // In obj-C one typically uses id instead of void*
Returning whole objects from methods has problems and it is better to just return a pointer (the address in memory) instead:
\- (NSString *)makeAString;
The method above returns a pointer to an NSString rather than the entire object.
Good luck with your learning,
Juan-Pablo
Message was edited by: Juan Pablo Claude