Override non-dynamic class delaration

2019-01-28 00:03发布

问题:

With new Xcode 8.3, I get error :

Cannot override a non-dynamic class declaration from an extension

in line of code

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

How I can avoid this warning?

回答1:

which means, you can't not override like super class delegate method in extension. the way you can do is mv extension override method to subclass declare.

final class DEMO, UITableViewDataSource, UITableViewDelegate { 
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return sectionHeader
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}

}



回答2:

because:

Extensions can add new functionality to a type, but they cannot override existing functionality.

you have 2 way:

1- add your method inside the class scope not extension

2- or add dynamic type for your method where you defined