Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

ios - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...