13

With new Xcode 8.3, I get the error:

Cannot override a non-dynamic class declaration from an extension

from the line of code:

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

How I can avoid this warning?

pkamb
  • 33,281
  • 23
  • 160
  • 191
UnRewa
  • 2,462
  • 2
  • 29
  • 31
  • see this http://stackoverflow.com/questions/41869177/cant-override-uitableviewdatasource-and-uitableviewdelegate – Anbu.Karthik Mar 29 '17 at 08:02
  • You avoid the warning by not overriding in an extension – do the override in the main body of the class. Compare http://stackoverflow.com/q/38213286/2976878 – Hamish Mar 29 '17 at 08:05

2 Answers2

17

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

You have two options:

  1. Move your method inside the Class-scope, instead of extension.

  2. Add dynamic type to your method (where it's defined), like example.

Example:

@objc open dynamic func onLog(_ message: String) -> Void {
    print("Info: \(message)");
}
Top-Master
  • 7,611
  • 5
  • 39
  • 71
jamal zare
  • 1,037
  • 12
  • 13
6

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
    }

}
Alirza Eram
  • 134
  • 11
looseyi
  • 79
  • 3