Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类 [英] Xcode 7 Swift 2 impossible to instantiate UIViewController subclass of generic UITableViewController

查看:173
本文介绍了Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个泛型类:

pre $ pa $ pa $ pa
$其中Source.PaginatedGenericElement == GenericElement>:
UITableViewController

另一个我尝试实例化from storyboard:

 类CandidatesTableViewController:
PaginatedTableViewController< Match,MatchPaginationDataSource>

我找不到 CandidatesTableViewController 故事板自定义类下拉菜单。如果我强制它,然后在代码中投我的控制器,应用程序崩溃在运行时抱怨我的控制器(这应该是一个CandidatesTableViewController实例)实际上是一个UITableViewController实例。


$ b


未知类_TtC21MyProjectName29CandidatesTableViewController in
Interface Builder文件。
无法将类型
'UITableViewController'(0x1040917f8)的值转换为
'MyProjectName.CandidatesTableViewController'(0x1013a9890)。

在我的项目中,这个控制器被嵌入到另一个中,这就是为什么我需要它:

  tableViewController =( segue.destinationViewController as!CandidatesTableViewController)

有没有人知道如何解决这个问题?

不幸的是,通用的Swift类对于Objective-C代码是不可见的,而且在Interface Builder(故事板和xibs)中也不受支持。我发现这两点密切相关。

作为一个解决方案,我建议你使用聚合:不要让你看到控制器通用,但提取一些逻辑到另一个(通用)类并在视图控制器中使用它。


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 ?

解决方案

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.

这篇关于Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆