Search code examples
iosswiftuitableviewinheritanceios-frameworks

Swift 3: Tableview datasource methods "Overriding non-open instance method outside of its defining module" error


I have an open BaseViewController class in core framework that has tableview datasource methods implemented. Let's say I've another class (outside the module) ClassA with BaseViewController as it's superclass. When I try to override tableview datasource methods, it's throwing this error Overriding non-open instance method outside of its defining module.

BaseViewController looks like this

open class BaseViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

...

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0
}

public func numberOfSections(in tableView: UITableView) -> Int {
    return 0
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return UITableViewCell()
  }
}

ClassA

import CustomCoreFramework

class ClassA : BaseViewController {

// throws an error
public override func numberOfSections(in tableView: UITableView) -> Int {
    return tableViewListItems.count
}

}

I suppose the open class methods should be accessible outside the module. I tried changing the tableview methods access specifiers to public and different combinations but nothing seems to work.


Solution

  • The BaseViewController’s methods should be declared open. This is discuss in the thread in reference.

    See What is the 'open' keyword in Swift?