1
votes

In Objective-C, is it considered good or bad practice to replace argument types with more specific (subclass) argument types in your implementation of a delegate/protocol method?

For instance, according to the documentation for UIGestureRecognizer:

The action methods invoked must conform to one of the following signatures:

- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

When I add a UITapGestureRecognizer to a view, I know that the argument to handleGesture: will be of type UITapGestureRecognizer (unless another gesture recognizer is added later). Is it frowned upon to replace the UIGestureRecognizer argument with an argument of type UITapGestureRecognizer? For example:

- (void)handleGesture:(UITapGestureRecognizer *)gestureRecognizer
{
    // implementation
}
1

1 Answers

2
votes

Protocols won't let you do this, because the argument types are part of the protocol specification. But you can, and often should, do this with action methods. I have seen code for action methods that looks like this:

- (void)somethingOrOtherAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    /* do something with |button| */
}

This code only works when the sender is a button, so there's no reason for the argument's type to be anything other than UIButton *. As far as the generated code is concerned, they're both pointers to objects, so one is as good as the other. But eliminating the cast makes the code cleaner while also revealing your intent.

The one thing that argues against this in your case is that, if you are not actually using anything that UITapGestureRecognizer offers over UIGestureRecognizer, then specifying the more specific type would be over-specifying the argument type relative to what the code actually expects. If the code would in fact work with a more general type, then you should consider using that more general type.