Quick Question: When to use ( ) and when to use [ ] and why the ( *)

Hi all,

A quick question that i'm sure is very simple but is slightly confusing me. I've just started trying to learn Objective-C and am a little confused by ( ) and [ ].

I get that you use the [ ] brackets when you want something specific from an object, i.e.
[ textField textColor ]

and that you use ( ) brackets for things like:

if ( x == y) {
...
}

But I get confused when I see things like:

NSLog(@"some text here");

Why does that get ( ) brackets and why is it not [ ].

Also another point of confusion, creating methods... Why are some methods done like:

- (void)awakeFromNib

And others done like (with the additional "*" added):

- (NSString *)stringvalue

As I said, I'm sure this is very simple and obvious, but it is confusing me slightly.

Thanks in advance!

iMac, Mac OS X (10.5.5), 2 Ghz, 4 GB RAM

Posted on Oct 5, 2008 4:07 AM

Reply
3 replies

Oct 5, 2008 1:17 PM in response to Adam Williamson

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

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Quick Question: When to use ( ) and when to use [ ] and why the ( *)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.