In objective-C, the concept of "function" is replaced with the concept of sending a message (identified by a "selector") to either a class object or to some object that is an instance of some class. When you define a "selector" that instantiated objects can respond to, the definition looks like this:
Code:
- (someReturnType)nameOfSelector: (arg1Type)arg1 nameContinued: (arg2Type)arg2 nameContinued: (arg3Type)arg3;
A specific example (taken from NSView) is
Code:
- (void)drawRect: (NSRect)theRect;
The selector is named "drawRect:". The trailing colon in the name indicates that an argument precedes the name. When you call that selector, there is no return value (void), and you call the selector using a single NSRect.
Another example (taken from NSColor) is
Code:
+ (NSColor *)colorWithCalibratedRed: (float)red green: (float)green blue: (float)blue alpha: (float)alpha;
Note the leading "+". This indicates that this selector represents a message that must be sent to the class object, not to a specific instantiated object belonging to that class. The "-" preceeding drawRect: meant that the drawRect: selector must be sent only to instantiated objects.
The selector name is "colorWithCalibratedRed:green:blue:alpha:" and when you send this message to the NSColor class, an instantiated NSColor object is returned, as in:
Code:
NSColor *myColor = [NSColor colorWithCalibratedRed: 0.5 green: 0.7 blue: 1.0 alpha: 1.0];
Which leads to your question about square brackets. They're how you send messages. The object you're sending to is the first item inside of the brackets (in this case, the singleton NSColor class object). The object is followed by the name of the selector interwoven with it's arguments. In the example above, when the message is received by the NSColor class object, it will be with red=0.5, green=0.7, blue=1.0 and alpha=1.0.
Likewise, to send the drawRect: message shown above, you could do something like
Code:
[myViewObject drawRect: someRectangle];
Since drawRect: must be sent to instantiated objects (the leading "-" in the selector definition), the object the message is sent to is something that's been instatiated (myViewObject). someRectangle is the NSRect that will be passed to theRect when the message is received.
I suspect I've probably confused you more, but this is actually pretty easy (and very very nice!!!) once you get accustomed to it. I've tried to use rigorous language, which has probably made things more confusing.
Anyway, for more info, check out
Apple's docs on the Objective-C language. Good luck!