当使用从Storyboard中的generics类inheritance的类时,“接口生成器文件中的未知类”

我最近BookTableViewController了我的类BookTableViewControllerUITableViewController一个简单的inheritance,所以它现在inheritance自一个generics类FetchedResultsTableViewController<TResultType, TCellType> ,它本身从UITableViewControllerinheritance。

类声明看起来像这样:

 class BookTableViewController: FetchedResultsTableViewController<Book, BookTableViewCell> { override func viewDidLoad() { // breakpoints in here do not catch! } } class FetchedResultsTableViewController<TResultType, TCellType: UITableViewCell>: UITableViewController, NSFetchedResultsControllerDelegate { // implementation here } 

在Storyboard中,Custom类和Module都被设置,我可以点击箭头跳转到BookTableViewController类的代码,这表明故事板正确链接到类。 但是,当我尝试运行该应用程序时,该类无法识别 – viewDidLoad()中的代码无法运行,并且在运行我的应用程序时收到以下logging的消息:

Interface Builder文件中的未知类_TtC12Reading_List23BookTableViewController。

我正在运行XCode 7.3(Swift 2.2)。 这是故事板的一个限制,一个错误,或者我错过了什么?

谢谢!

更新

经过一番实验,似乎与genericsinheritance有关,而不是类的可访问性。 定义了以下类:

 import Foundation import UIKit // Both of these classes are accessible by the Storyboard class FirstInheritance : UITableViewController{} class SecondInheritance : FirstInheritance{} // The generic class is also accessible class GenericViewController<T> : UITableViewController{} // But this class is not accessible... class ConcreteViewController : GenericViewController<String>{} 

在Storyboard的类自动完成中,除了ConcreteViewController之外的所有类都被build议(尽pipegenerics类也不起作用,因为没有办法在Storyboard中指定types参数)。

游戏迟到了一点,但是这个信息可能会适用于任何人碰到线程。

实际上,据我所知,从Swift 3开始,现在在故事板中出现了一些对仿制药的尴尬支持。

我已经设法编写一个扩展UIViewController的generics基类,然后约束该generics类的几个子类,并在故事板中使用它们。

实际的代码与以下规定的布局类似:

 class GenericParent: UIViewController { // Has a few, non-generic members ... } class ActualGenericClass<T>: GenericParent { // There are more generic members in the actual class var adapter: Adapter<T> = Adapter() } // This class is usable in Interface Builder; // although auto-complete won't show it, fully writing down // the class name works class SpecificInstanceOfActualGenericClass: ActualGenericClass<Int> { @IBOutlet weak var tableView: UITableView! } 

这完美的作品,让我吃惊!

另一方面,下一个例子似乎不工作:

 class GenericCell<T>: UITableViewCell { var node: T? = nil } class SpecificCell: GenericCell<Int> { @IBOutlet weak var coolTitleLabel: UILabel! } 

和以前一样,在Interface Builder上(在单元的dynamic原型上)显式地写入单元的类名,在表格视图单元上设置类,并且出口是可见的。

但是在运行时,当实例化包含单元的dynamic原型的UIViewController时,会显示“界面生成器文件中的未知类”警告,并且应用程序在出现单元时出现故障。

所以@Andew Bennet,声明:

故事板不支持从generics类inheritance的类

似乎不再是100%,尽pipe你至less是正确的; 这是我第一次把这个关掉,尽pipe只是部分!

它让我感到有些东西是有效的,有些则没有,但总比没有好。

这在Java中是非常直截了当的

故事板不支持直接或间接从genericsinheritance的类。 如果使用从故事板中的generics类inheritance的类,那么在运行时会出现错误,指出该类未知。

另一种方法是使用带有types和扩展名的协议:

 protocol MyProtocol { associatedtype genericType1 associatedtype genericType2 func myFunc(argument1: genericType1, argument2: genericType2) } extension MyProtocol { func defaultFunc(argument1: genericType1){ // default implementation here } } 

该协议使用如下:

 class NonGenericClass: MyProtocol { typealias genericType1 = Int typealias genericType2 = String func myFunc(argument1: genericType1, argument2: genericType2){ // specific implementation here } } 

NonGenericClass类将具有MyProtocol扩展中的所有函数(在本例中为defaultFunc(argument1: Int) )。

协议MyProtocol也可以inheritance其他协议, 这些函数的实现可以在MyProtocol的扩展中MyProtocol 。 因此,协议可以使任何符合它的类也符合另一个协议,并具有标准的实现。

但是,这有一个限制,就是你不能在协议中指定类的功​​能的标准覆盖。