[英]Swift - Generic that subclasses and conforms to a protocol
I basically have this protocol 我基本上有这个协议
protocol ReusableView {
static var reuseIdentifier: String { get }
}
and this generic class 和这个通用类
class ListController<Item: Equatable, Cell: UITableViewCell>: UIViewController where Cell: ReusableView {
private var items: [Item]
init(items: [Item]) {
self.items = items
super.init(nibName: nil, bundle: nil)
print(Cell.reuseIdentifier)
}
}
When I try to print the reuseIdentifier, I get this compiler error 当我尝试打印reuseIdentifier时,出现此编译器错误
Instance member 'reuseIdentifier' cannot be used on type 'Cell'
实例成员'reuseIdentifier'不能用于'Cell'类型
I should be able to access the property since the Cell Object conforms to the ReusableView protocol. 由于单元对象符合ReusableView协议,因此我应该能够访问该属性。
I don't know what the problem is. 我不知道问题是什么。 Any help would be appreciated.
任何帮助,将不胜感激。
Thanks 谢谢
The problem is occurring because Cell
must inherit from UITableViewCell
which already defines an instance variable reuseIdentifier
. 发生此问题是因为
Cell
必须继承自UITableViewCell
,而UITableViewCell
已经定义了实例变量reuseIdentifier
。 Simply changing the name of reuseIdentifier
to reusableViewIdentifier
or something will cause the error to go away. 只需将
reuseIdentifier
的名称更改为reusableViewIdentifier
或类似的东西,就会导致错误消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.