3

Something really weird is going on after our Swift 3 migration. We have two view controllers, both of them implement UITableViewDelegate and both of them implement public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

However only in one of them the actual method is called.

If I change in the one that doesn't work public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) to public func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath) (notice the Swift 2.2 signature) then they both work.

Both view controllers are Swift classes, so I am not sure what the heck is going on. I am pretty sure it might be a Objective-C vs Swift interoperability issue, but our whole project is written in Swift, so that's why it's hard to figure out what is causing this.

Any help is appreciated. Thank you.

Cosmin
  • 2,840
  • 5
  • 32
  • 50
  • How did you put ```didSelectRowAtIndexPath``` in Swift 3 ? It is removed in Swift 3. You can only use ```didSelectRowAt``` ? –  Sep 26 '16 at 12:34
  • It's declared just like in Swift 2.2 and it compiles just fine. – Cosmin Sep 26 '16 at 14:14
  • I had a similar problem with some collectionView delegate functions. Although everything was fine they didn't work. After commenting out the non working functions and adding the functions again through XCode auto completion it worked just well - just to mention that there was no typo mistake between both functions..... I just copied and pasted my code from the old function into the new one and voila... maybe it helps – Armin Scheithauer Sep 26 '16 at 16:01

2 Answers2

8

for Swift 3.0, use

override  func tableView(_ tableView: UITableView, didSelectRowAt
 indexPath: IndexPath){

  //your code...  


 }
Ash
  • 5,525
  • 1
  • 40
  • 34
1

I encountered a similar problem. My problem was caused by having a superclass that adopted the UITableViewDelegate and UITableViewDataSource protocols, and then implementing the actual methods in a subclass.

What I gather is that because UITableViewDelegate and UITableViewDataSource are objective-c protocols, they must be adopted directly by the class implementing these functions. Otherwise the Swift function signatures will not be properly mapped to the objective-c function signatures (not sure why this is the case).

In Swift versions prior to 3.0, the underlying objective-c function signatures matched the signatures in the Swift UITableViewDelegate and UITableViewDataSource protocols. Therefore prior to 3.0 it seems to have worked fine to have a superclass adopt these protocols. However as of Swift 3.0 these signatures are no longer a match. It seems that to have the new-style signature properly mapped to the underlying objective-c signature, your class must directly adopt the UITableViewDelegate and UITableViewDataSource protocols.

Therefore in Swift 3.0 and later, if you do not directly adopt the UITableViewDelegate and UITableViewDataSource protocols then your function signatures must match the old-style underlying objective-c signatures in order for your functions to be called correctly.

dougzilla
  • 156
  • 1
  • 5