开发者

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

开发者 https://www.devze.com 2023-03-24 03:28 出处:网络
This would be useful for consistently naming methods, for example let\'s take a look at some method names from the UITableViewDataSource protocol:

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开发者_C百科 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?


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;


I'm afraid the answer is no.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消