And*_*net 6 xcode uikit ios swift xcode7
最近我重构我的课BookTableViewController从一个简单的继承UITableViewController,所以它现在从一个普通的类继承FetchedResultsTableViewController<TResultType, TCellType>其本身继承UITableViewController.
类声明如下所示:
class BookTableViewController: FetchedResultsTableViewController<Book, BookTableViewCell> {
override func viewDidLoad() {
// breakpoints in here do not catch!
}
}
class FetchedResultsTableViewController<TResultType, TCellType: UITableViewCell>:
UITableViewController, NSFetchedResultsControllerDelegate {
// implementation here
}
Run Code Online (Sandbox Code Playgroud)
在Storyboard中,Custom类和Module都已设置,我可以单击箭头跳转到BookTableViewController类的代码,建议故事板正确链接到类.但是,当我尝试运行应用程序时,无法识别该类 - 代码输入viewDidLoad()未运行,并且在运行我的应用程序时收到以下记录的消息:
Interface Builder文件中的未知类_TtC12Reading_List23BookTableViewController.
我正在运行XCode 7.3(Swift 2.2).这是故事板的限制,一个错误,还是我错过了什么?
谢谢!
更新:
经过一些实验,它似乎与通用继承有关,而不是类的可访问性.定义了以下类:
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>{}
Run Code Online (Sandbox Code Playgroud)
除了ConcreteViewController在Storyboard的类自动完成中建议的所有类(尽管泛型类也不起作用,因为无法在Storyboard中指定类型参数).
小智 5
游戏进行得不算太晚,但是对于任何碰到线程的人来说,此信息可能会派上用场。
实际上,据我所知,从Swift 3开始,对故事板中的泛型有某种尴尬的支持。
我设法编写了一个扩展UIViewController的通用基类,然后限制了该通用类的几个子类,并在情节提要中使用了它们。
实际的代码与下面指定的布局类似:
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!
}
Run Code Online (Sandbox Code Playgroud)
这很完美,令我惊讶!
另一方面,下一个示例似乎不起作用:
class GenericCell<T>: UITableViewCell {
var node: T? = nil
}
class SpecificCell: GenericCell<Int> {
@IBOutlet weak var coolTitleLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
像以前一样,在Interface Builder(在单元的动态原型上)上显式编写单元的类名称将在表视图单元上设置该类,并且出口可见。
但是,在运行时,实例化包含单元格动态原型的UIViewController时,将显示“接口生成器文件中的未知类”警告,并且使单元出队时App崩溃。
因此,@ Andew Bennet,声明如下:
故事板不支持从通用类继承的类
尽管您至少是正确的,但似乎不再是100%正确的了;这是我第一次设法做到这一点,尽管只是部分!
让我感到困惑的是,有些事情行之有效,而有些则行不通,但这总比没有好。
这在Java中是如此简单明了...
| 归档时间: |
|
| 查看次数: |
1356 次 |
| 最近记录: |