Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSObject classes inside Extensions have undefined behaviors? [duplicate]

We have a big issue with the current Xcode version (10.2).

There is a BasicViewController class with the following signature:

class BasicViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Then we have another class like ExampleViewController: BasicViewController which has some additional logic.

Now comes the tricky part...

We build and run the app on different simulators and devices and everything works properly. Then we archive the app and suddenly didSelectRow is not fired anymore. Deep clean and clean of the project allowed us to reproduce the issue without the need to archive again.

I cannot think of any case when this could happen. And it gets even worse, because I found more similar issues with UITableViewDelegate methods not being called in the child class only when running the archived app. Could it be an issue with some of the optimisations during archiving and submitting the app?

I verify that we set the dataSource and delegate of the table properly, there are no gesture recognisers over the table. The same logic works well after running the app second/third time, but fails first time after a deep clean of the project.

We made a test and set the UITableViewDataSource and UITableViewDelegate in the child class and then it works as expected every time. It seems inheriting the protocols does not work well. If we keep the protocols in the parent and also add them in the child class, then it says that the protocols in the child class are redundant.

Has anyone experienced anything similar? Any suggestions are welcome.

like image 961
o15a3d4l11s2 Avatar asked Apr 09 '19 11:04

o15a3d4l11s2


2 Answers

You may be running into https://bugs.swift.org/browse/SR-10257 in the Swift 5.0 compiler. This would happen if you have at least three files:

  1. BasicViewController.swift
  2. SomeOtherFile.swift
  3. ExampleViewController.swift

If SomeOtherFile.swift makes any calls to an AnyObject-typed value, you're compiling in wholemodule mode, and the files are passed to the compiler in the above order (with SomeOtherFile.swift in the middle of the two), then it seems that the compiler fails to properly infer @objc-ness for the implementation of func tableView(_:, didSelectRowAt:). You can work around it by explicitly tagging it with @objc for now.

like image 127
BJ Homer Avatar answered Oct 12 '22 18:10

BJ Homer


I ran into the same issue. I fixed it by adding the methods directly in my main class, and override them in the other classes. Now everything gets called correctly.

like image 45
J. Doe Avatar answered Oct 12 '22 19:10

J. Doe