Swift 3.0 UITableViewDelege Objective-c method does not meet selector requirement

I recently converted a project to Swift 3 with Xcode 8.0, and I got an error in a function that I don't understand very well. In these lines:

extension HomeTableViewController : UITableViewDelegate { func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { } } 

To fix this error, Xcode tells me to add @objc(tableView:commitEditingStyle:forRowAtIndexPath:) immediately before this method.

Xcode Error

Well, this works, but I don’t understand why this is only required for this method.

Xcode does not require adding an @objc element in front of my tableView:heighForHeaderInSection , but I see no difference in the UITableViewDelegate between this method and tableView:commitEditingStyle:forRowAtIndexPath:

So, you know why this is necessary for the tableView:commitEditingStyle:forRowAtIndexPath ?

Thanks in advance! πŸ˜‰

+7
swift swift3
source share
1 answer

You are accepting the wrong protocol in your extension. The tableView:commitEditingStyle:forRowAtIndexPath: is part of the UITableViewDataSource protocol. Change the extension to accept the UITableViewDataSource protocol instead of the UITableViewDelegate protocol, and the error will disappear.

At least this worked for me when I got this error with the NSCollectionViewDelegate/DataSource extensions.

+10
source share

All Articles