2
\$\begingroup\$

I'm aware standard delegate method declaration should include the object that triggers the method, usually of the class that declared the protocol.

That's fine when the delegate method includes further arguments down the line, but what if it doesn't?

Example:

@protocol MyObjectDelegate <NSObject>
- (void)myObject:(id)object didPerformActionWithResult:(NSObject*)resul;
- (void)myObject:(id)object didPerformActionWithoutResult;
@end

I know the second method isn't accepted, its missing an argument, but what do I put there? A BOOL that I know will always be true?

Do I change |didPerformActionWithoutResult| to performedAction:(BOOL)yesOrNo?

Or move the object reference to the end?

asked Apr 21, 2012 at 12:03
\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

Have a look at TableView's datasource protocol:

– tableView:cellForRowAtIndexPath:
– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:
– sectionIndexTitlesForTableView:

I find it quite hard to come up with nice names for such sidle parameter method signature and code completion would be easier, if the form, you propose would exists, but if a method accepts parameters, it name must terminate with one of therm — if it just have one, it must be at the end.

Your idea of adding a dead parameter is IMO very bad design. Another parameter should lead possibly to another behavior. Also it would work against objective-c strength of self explaining method name. countOfSubscribersForNewsEmitter:(MyNewsEmitter *)emitter make sense, while newsEmitter:(MyNewsEmitter *)emitter countOfSubscribers:(BOOL) aBool does not make much sense, as the bool disallows us to form a correct mental model of the purpose of this method.

answered Apr 22, 2012 at 16:01
\$\endgroup\$
1
  • \$\begingroup\$ Seconded. Copy Apple's naming conventions like this one so your code is easier to understand and fits with the rest of the API. :) There's no great answer to this question, so What Apple Did is the best solution. \$\endgroup\$ Commented Apr 26, 2012 at 2:08
2
\$\begingroup\$

This might indeed be confusing at first. The solution is to put the calling object as the only and last argument of the method. The second delegate method would then look like the one below.

- (void)myObjectDidPerformActionWithoutResult:(id)object;
answered Apr 22, 2012 at 12:14
\$\endgroup\$
2
\$\begingroup\$
- (void)actionPerformedWithoutResultByObject:(id)object;

might be another alternative.

answered Aug 15, 2012 at 21:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.