【问题标题】:Swift - Generic that subclasses and conforms to a protocolSwift - 子类化并符合协议的泛型
【发布时间】:2017-01-04 22:31:35
【问题描述】:

我基本上有这个协议

  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”不能用于类型“Cell”

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

我不知道问题是什么。 任何帮助,将不胜感激。

谢谢

【问题讨论】:

标签: swift generics protocols


【解决方案1】:

出现问题是因为Cell 必须继承自UITableViewCell,而UITableViewCell 已经定义了一个实例变量reuseIdentifier。只需将 reuseIdentifier 的名称更改为 reusableViewIdentifier 或其他东西就会导致错误消失。

【讨论】:

    猜你喜欢
    • 2014-12-09
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多