Swift - 子类并符合协议的泛型

时间:2017-01-04 22:31:35

标签: swift generics protocols

我基本上有这个协议

  protocol ReusableView {
     static var reuseIdentifier: String { get }
  }

和这个通用类

  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)
      }
  }

当我尝试打印reuseIdentifier时,我收到此编译器错误

  

实例成员&#39; reuseIdentifier&#39;不能用于&#39; Cell&#39;

我应该能够访问该属性,因为Cell对象符合ReusableView协议。

我不知道问题所在。 任何帮助,将不胜感激。

由于

1 个答案:

答案 0 :(得分:3)

问题出现是因为Cell必须从已定义实例变量UITableViewCell的{​​{1}}继承。只需将reuseIdentifier的名称更改为reuseIdentifier或其他内容,就会导致错误消失。

相关问题