0

Can swift language permit many functions with the same name in the protocol or in a class? Forgive me. Thanks.

protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {

// Display customization

optional func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
@availability(iOS, introduced=6.0)
optional func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
@availability(iOS, introduced=6.0)
optional func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int)
@availability(iOS, introduced=6.0)
optional func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
@availability(iOS, introduced=6.0)
optional func tableView(tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int)
@availability(iOS, introduced=6.0)
optional func tableView(tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)

// Variable height support

optional func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
optional func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
optional func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
NorthBig
  • 47
  • 1
  • 9

1 Answers1

0

This is possible because Swift supports function overloading. You'll notice that each of while each of those methods have the same name, they accept different named parameters.

You can use this to your advantage in your own code to provide a consistent API that can support numerous types. I recently used this functionality to extend a class's subscript operator to accept my own enum type rather than only accepting strings.

In the case of UITableViewDelegate, it is essentially identical to it's Objective-C counterpart as Apple doesn't want to rewrite everything for Swift. It's highly likely that the API would have been different if they were to write this from scratch for Swift.

Arkcann
  • 618
  • 7
  • 15