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

此生再无相见时 提交于 2019-12-17 14:53:15

问题


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 ?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/34534854/xcode-7-swift-2-impossible-to-instantiate-uiviewcontroller-subclass-of-generic-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!