6
votes

I have a generic class:

class PaginatedTableViewController
  <GenericElement, Source: PaginationDataSource 
     where Source.PaginatedGenericElement == GenericElement>:
  UITableViewController

and another that I try to instantiate from storyboard:

class CandidatesTableViewController: 
   PaginatedTableViewController<Match, MatchPaginationDataSource>

I can't find CandidatesTableViewController in the storyboard Custom Class dropdown menu. If I force it then cast my controller in code, app crashes at runtime complaining my controller (that should be a CandidatesTableViewController instance) is in fact a UITableViewController instance.

Unknown class _TtC21MyProjectName29CandidatesTableViewController in Interface Builder file. Could not cast value of type 'UITableViewController' (0x1040917f8) to 'MyProjectName.CandidatesTableViewController' (0x1013a9890).

In my project this controller is embedded in another one that's why I cast it :

tableViewController = (segue.destinationViewController as! CandidatesTableViewController)

Does any one knows how to resolve this issue ?

2

2 Answers

11
votes

Unfortunately, generic Swift classes are not visible to Objective-C code and also are not supported in Interface Builder (in storyboards and xibs). I find these two points closely related.

As a solution I would suggest you to use aggregation: do not make you view controller generic, but extract some logic to another (generic) class and use it inside your view controller.

0
votes

It is possible if you manually load your generic VC into Objective-C runtime manually via the load() method i.e. call. PaginatedTableViewController.load() in your app delegate's init method. Idea from https://stackoverflow.com/a/43896830/671580