1

You can indicate that XXX is method name by writing XXX().

But in Objective-C, you can not write XXX() for XXX method.

If you write XXX, you can't tell if XXX is method name or other type of identifier.

How can you indicate that XXX is method name of Objective-C class?

EDIT

Method call of Objective-C is different from other language.

in Objective-C, method call is:

[self XXX];

in other language, method call is:

self->XXX();

In other language, for example, we can write in stackoverflow:

XXX() doesn't work

And we can know XXX is method thanks to ().

But in Objective-C, we can't say:

XXX doesn't work

Instead, to make it clear that XXX is method, we have to say:

XXX method doesn't work
3
  • Could you post some sample code to illustrate the problem more clearly? Also, could you name it initMethod() to clear up the confusion? Commented Jul 20, 2012 at 15:01
  • @FrustratedWithFormsDesigner thanks for your advice. I added a explanation. And init is just a example. To avoid confusion, I modified it to XXX.
    – js_
    Commented Jul 20, 2012 at 15:27
  • You can tell it's a method because it's on the right side of the message call. In objective-c it's [object method] or [Class method]. If you are confusing properties, you have to remember that properties are just sugar.
    – Paul
    Commented Jul 20, 2012 at 15:51

3 Answers 3

3

If you're talking about using init in documentation or some other place where you're describing the method, you can prefix it with a - if it's an instance method or a + if it's a class method. If the method takes parameters, leave them out and just concatenate the different parts of the method name, like: -initWithNibName:bundle: or +colorWithRed:green:blue:alpha:. Don't leave the colons out, though -- they're part of the selector. For example, you might write:

One way to create a color is by calling UIColor's +colorWithRed:green:blue:alpha: method.

Sometimes people include both the class name and method name, prefixed with the appropriate character and wrapped in square brackets:

One way to initialize a view controller is with -[UIViewController initWithNibName:bundle:].

That's not really a correct use of Objective-C syntax -- you'd never actually call a method like that -- but it conveys three important pieces of information: type of method (class or instance), class, and selector.

In code, of course, you don't need to do anything special to indicate what kind of identifier it is. Just use the method name in an appropriate context and the compiler will figure it out:

UIColor *color = [UIColor colorWithRed:0.5 green:0.5 blue:0.9 alpha:1.0];

NSMutableArray *array = [[NSMutableArray alloc] init];
0
4

I will typically use +[NSString stringWithString:] and -[NSString initWithString:], unless there is enough context to omit the + or - and class (then I would reduce it to stringWithString: and initWithString:).

1

Terminology: It's not a method name, it's a message name, aka selector.

To your question: it's obvious from the syntax, message sends always take the form [receiver message_with_parameters], where receiver is an expression (it may be another [...]), so anything after receiver is a selector (that is, what you call method name). In case of selector without params, it is just its name (like is the case with [[Class alloc] init], in case of of selector with params, it looks like [3 between: 1 and: 5].

Or is your problem somewhere else? The question was a bit unclear.

2
  • thanks. But i'm very sorry, my question was not clear. I added EDIT to my question. My question is about how to write method name, for example, in stackoverflow to let other people know that it is method name.
    – js_
    Commented Jul 20, 2012 at 15:33
  • 1
    In that case, use 'method Xxx' or use the '- selector' and '+ selector' syntax of Objective-C used to distinguish between instance and class methods.
    – herby
    Commented Jul 20, 2012 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.