ios - Xcode 7 Swift 2 无法实例化通用 UITableViewController 的 UIViewController 子类

标签 ios swift generics swift2

我有一个泛型类:

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

还有一个我尝试从 Storyboard 中实例化的:

class CandidatesTableViewController: 
   PaginatedTableViewController<Match, MatchPaginationDataSource>

我在 Storyboard Custom Class 下拉菜单中找不到 CandidatesTableViewController。如果我强制它然后在代码中转换我的 Controller ,应用程序会在运行时崩溃并提示我的 Controller (应该是 CandidatesTableViewController 实例)实际上是一个 UITableViewController 实例。

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

在我的项目中,这个 Controller 嵌入到另一个 Controller 中,这就是我转换它的原因:

tableViewController = (segue.destinationViewController as! CandidatesTableViewController)

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

最佳答案

不幸的是,通用 Swift 类对 Objective-C 代码不可见,并且在 Interface Builder 中(在 Storyboard和 xib 中)也不支持。我发现这两点密切相关。

作为一种解决方案,我建议您使用聚合:不要让您的 View Controller 通用,而是将一些逻辑提取到另一个(通用)类并在您的 View Controller 中使用它。

关于ios - Xcode 7 Swift 2 无法实例化通用 UITableViewController 的 UIViewController 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534854/

相关文章:

swift - 如何在swift中制作圆角进度条?

xcode - 使用数组和 UITableView 作为指示器通过函数调用字符串

java - 有人可以向我解释基本的 Java 泛型吗?

c# - 具有泛型参数的扩展方法到泛型类

c# - 在不指定 T 的情况下模拟 Moq 中的泛型方法

ios - iOS 和 OSX 使用相同的 Objective-C 运行时吗?

ios - 取消按钮在 UIImagePickerView 中不起作用

ios - UIImagePickerController 在 Swift 3 的图像编辑模式下更改底部栏按钮标题

ios - 在 iOS 上,是什么让 libxml2 Public 而 libxslt Private?

ios - UIWebViewDelegate 在 ARC 中手动发布?