tags:

views:

301

answers:

3

I am wondering why in Books and Apple Documentation sometimes a Method has that colon as suffix, but sometimes not. I guess it means that if it has no colon, it has no parameter. On the other side, if it has a colon, it has exactly one parameter. Right or wrong?

+1  A: 

That's correct, though it is an easy typo to make. You should always check the documentation to ensure the signature of any method to avoid any runtime errors.

A method with signature:

- (void)refresh

Will be used like:

[myObject refresh];

A method with signature:

- (void)refreshView:(UIView *)view

Will be used like:

[myObject refreshView:view];

And finally, a method with signature:

- (void)refreshView:(UIView *)view updateLabels:(BOOL)update

Will be used like:

[myObject refreshView:view updateLabels:YES];
craig
Thanks. But does it make sense that someone would write in an documentation text "refreshView:" instead of "refreshView:view" or just "refreshView"?
Thanks
It would if there is only one method that appeared that way. If an API had all three methods above, for example, then the author should be very careful to specify which one he means exactly. But if there was only one refreshView method, and no others that took parameters, it's generally acceptable.
craig
A: 

Objective-C refreshView: and refreshView are two different methods. The first takes one parameter, the other takes no paramaters. As you say.

This is important because that is the full name of the method, and you need to be able to write this correctly when passing selectors.

For example when showing a sheet:

- (void)beginSheet:(NSWindow *)sheet 
    modalForWindow:(NSWindow *)docWindow 
    modalDelegate:(id)modalDelegate 
    didEndSelector:(SEL)didEndSelector 
    contextInfo:(void *)contextInfo;

the didEndSelector is usually of the form:

- (void)sheetDidEnd:(NSWindow *)sheet 
    returnCode:(int)returnCode 
    contextInfo:(void *)contextInfo;

and so in the beginSheet method this will need to be passed to the didEndSelector parameter as:

@selector(sheetDidEnd:returnCode:contextInfo:);

Getting the signature of the selector wrong will lead to much late night head scratching while debugging.

Abizern
that pretty much looks like if an method in objective-c has actually no method name, bot consists only of parameter names, if it has parameters. and if it has none, then it has a method name. is that a valid statement?
Thanks
I don't think it is. Don't think of it as a list of parameter names, rather, a really well named function. It really is worth spending more than a moment's thought when creating function names.
Abizern
It's technically still a "method name", colons and all -- it's just that you intersperse bits of the name and arguments when you call them. Note that this is different from languages that truly have named parameters (Common Lisp, Python, etc), where you can change the order of arguments.
Daniel Dickison
A: 

You're right that the trailing colon signifies a single parameter, and it's important to use the full including-colon name in code -- e.g. @selector(drawRect:)

However, while I can't find an example off hand, in prose, I believe you'll occasionally see methods written without the trailing colon just to make it read better. I know I do this when writing comments/documentation -- e.g. "Subclasses should customize the doFoo method" when I actually mean doFoo:. So, if you see method names in prose, it's probably a good idea to check in the header file or class reference documentation for the correct signature.

Daniel Dickison
thanks. Well I'm not sure what Apple does in the Docs, but I hope they're consistent with how they write it. Because sometimes I see that colon, and sometimes not. Althoug in different methods. Good idea to check it in the class reference.
Thanks