jhabbott
jhabbott

Reputation: 19311

In Obj-C is it possible to have no final parameter after the method name?

This would be useful for consistently naming methods, for example let's take a look at some method names from the UITableViewDataSource protocol:

numberOfSectionsInTableView:
tableView:heightForHeaderInSection:
tableView:viewForHeaderInSection:
tableView:heightForFooterInSection:
tableView:viewForFooterInSection:
tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:
sectionIndexTitlesForTableView:

Notice how the first and last ones are odd - they both still take a UITableView* as the first parameter, but because it is the only parameter the consistency of the naming convention is spoiled. It would be much nicer if they could be named like this:

tableView:numberOfSections
tableView:sectionIndexTitles

Is it possible to define methods like this, or similar? I have tried these:

- (NSUInteger)specialView:(SpecialView*)specialView numberOfThingies;
- (NSUInteger)specialView:(SpecialView*)specialView numberOfThingies:(void)unused;

The first is ideal, but causes an error in the header file. The second is not ideal (would be better without the colon at the end) but it is accepted in the header, I can't figure out how to call it though. I've tried variations of:

[anObject specialView:aSpecialView numberOfThingies];
[anObject specialView:aSpecialView numberOfThingies:];
[anObject specialView:aSpecialView numberOfThingies:void];
[anObject specialView:aSpecialView numberOfThingies:(void)0];

Is there a way to do what I want to do? Are there any other sensible naming conventions anyone has devised that keep such methods consistent?

Upvotes: 3

Views: 127

Answers (3)

Dancreek
Dancreek

Reputation: 9544

What you are trying to do is not possible. You can have a function that does not take arguements at all but if you take any argueements you cannot add anything after the arguments.

Upvotes: 0

Daniel Dickison
Daniel Dickison

Reputation: 21882

I'm afraid the answer is no.

Upvotes: 0

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

Short answer: no, it's not possible in the current version of Objective-C. You can end a method without a colon and argument only if there are no arguments at all; otherwise, any part of the method name must be followed by an argument.

To do what you want, you'll have to follow the pattern used by UITableView. One of the following will be necessary:

- (NSUInteger)specialViewNumberOfThingies:(SpecialView *)specialView;
- (NSUInteger)numberOfThingiesInSpecialView:(SpecialView *)specialView;

Upvotes: 6

Related Questions