Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3.0 UITableViewDelege Objective-c method does not match the requirement's selector

Tags:

swift

swift3

I recently converted a project to Swift 3 with Xcode 8.0 and I got a error on a function which I don't understand very well. On these lines:

extension HomeTableViewController : UITableViewDelegate {

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    }
}

To resolve the error, Xcode tells me to add @objc(tableView:commitEditingStyle:forRowAtIndexPath:) just before the method.

Xcode error

Okay, it works, but I don't get why it is only required for this method.

Xcode doesn't require to add the @objc stuff in front of my tableView:heighForHeaderInSection but I don't see any differences in the UITableViewDelegate between this method and the tableView:commitEditingStyle:forRowAtIndexPath:.

So, know why is this mandatory for the tableView:commitEditingStyle:forRowAtIndexPath method ?

Thanks in advance! 😉

like image 287
Toldy Avatar asked Sep 15 '16 12:09

Toldy


1 Answers

You are adopting the incorrect protocol in your extension. The tableView:commitEditingStyle:forRowAtIndexPath: method is part of the UITableViewDataSource protocol. Change your extension to adopt the UITableViewDataSource protocol instead of the UITableViewDelegate protocol and the error goes away.

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

like image 152
JWDzubak Avatar answered Oct 05 '22 03:10

JWDzubak