user1219702
user1219702

Reputation: 221

Overloading methods is not supported in Objective-C

I know that Objective-C does not support method overloading. However, how can I understand the following delegate methods with the same name as 'tableView'? To me, those methods seem to be overloading ones, but I am not sure.

For a view controller to indicate that it is a UITableView delegate, it must implement the UITableViewDelegate protocol. The following are common delegate methods to implement in the view controller:

tableView:heightForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
tableView:didDeselectRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:

Upvotes: 1

Views: 956

Answers (1)

occulus
occulus

Reputation: 17014

All the methods you list are different, because the selector name includes all the parts, not just the part up to the first colon ':'.

Here is an example of attempted method overloading in Objective C (which the compiler will reject):

- addSomething:(NSObject *) toView:(UIView *)view
- addSomething:(UIView *) toView:(UIView *)view   // won't work

Note that you are allowed to 'overload' a method when there is a class method and an instance method variant with the same name:

- addSomething:(NSObject *) toView:(UIView *)view 
+ addSomething:(NSObject *) toView:(UIView *)view  // this is OK

Obviously you'd want quite a good reason to do something potentially confusing like this!

See also this question:

Class method and instance method with the same name in Objective-C

Upvotes: 7

Related Questions