Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 7 Swift 2 impossible to instantiate UIViewController subclass of generic UITableViewController

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 ?

like image 440
Pentagp Avatar asked Dec 30 '15 18:12

Pentagp


1 Answers

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.

like image 88
werediver Avatar answered Oct 24 '22 07:10

werediver