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
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
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:
).
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