Subclasses of Swift Generic Classes and Obj-C

We're having a generic swift class that acts as a UITableView data source and delegate. Not all of the methods of the UITableViewDelegate are implemented in this generic swift class. We're now trying to create a subclass of our generic swift class that implements a few extra UITableViewDelegate methods. Those, are, however not visible in Obj-C:

class DataSource<T>: NSObject, UITableViewDelegate {
	let items = Array<T>()
}

class SubClassDataSource<T>: DataSource<T> {
    @objc func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {return 44}
}

let dataSource = SubClassDataSource<Int>()
print(dataSource.responds(to: #selector(UITableViewDelegate.tableView(_:heightForRowAt:)))) // prints false

I would expect true to be printed here, since the method is marked as obj-c explicitly. Is there a rational for this? How would I make this work?

(The reason we have this set up is for performance: overriding -tableView: heightForRowAt: for very large tableviews is slow, so we rather do it in a specific subclass to not incur the performance penalty)

I just came up against this problem, too. Thanks to this answer on StackOverflow I was able to get things working be explicitly putting the name of the ObjC version of the method:

@objc (tableView:heightForRowAtIndexPath:)
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {return 44}

If anyone's aware of a relevant bug in Jira or proposal in swift-evolution, I'd be interested to follow along.

Sounds like SR-9479.

Thanks - unfortunately a very longstanding Swift bug, which so far we've hit a lot of. We're actually thinking of moving parts of our code back to Obj-C, so we're not hit by Swift's instability ;-(

Terms of Service

Privacy Policy

Cookie Policy