Search code examples
swiftuitableviewuiviewcontrollerswift3assert

Assert that viewController is both delegate and dataSource of its tableView


As a sanity check, I would like to assert in viewDidLoad that a UITableViewController (i.e. self) is set both as dataSource and delegate of its tableView. How can I put this in Swift 3, where (unlike with Objective-C) additional type casts are needed? This still leads to syntax errors:

assert(tableView.dataSource == self)
assert(tableView.delegate   == self)

Solution

  • You can do this using the object instance equality operator '===':

    assert(tableView.dataSource === self)
    assert(tableView.delegate === self)