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自定义类下拉菜单中找不到 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/43403058/

相关文章:

ios - Dropbox 委托(delegate)方法不是第一次调用

swift - 在 UserDefaults 中保存自定义对象数组时出现 NSInvalidArgumentException

ios - TableView 上方的 float /固定搜索栏 - Swift 3

ios - Swift 的 AGPushNote 替代品?

Java ArrayList indexOf 泛型类型

ios - Service Worker FechtEvent.respondWith 响应在 iOS 12.1 Safari 上为空

ios - 使用 linux 为 IOS 编译 TOR

ios - 在 iOS 应用程序中跨多个 View Controller 共享某些数据的最佳方式是什么

C# Language : generics, open/closed, bound/unbound, constructed

java - 删除类型和桥接方法澄清