一般问题,任何建议?

时间:2018-05-30 12:48:28

标签: ios swift generics protocols

// UITableView + .Swift Extension

extension UITableView {

 func cellForModel<T: CellViewModel>(at: IndexPath, model: T) -> T.CellType  {
    let cell: T.CellType = dequeueReusableCell()
    *let t = model as! T.CellType.ModelType*
    cell.setupModel(model: t)
    return cell
  }

  func dequeueReusableCell<T: ReusableCell>() -> T {
    print(T.reuseIdentifier())
    let temp = self.dequeueReusableCell(withIdentifier: T.reuseIdentifier())

    return temp as! T
  }
}

// CellViewModel.Swift

protocol CellViewModel where Self: NSObject, CellType: ReusableCell {
  associatedtype CellType
}

// ReusableCell.Swift

protocol ReusableCell where Self: UITableViewCell, ModelType: CellViewModel {
  associatedtype ModelType
  static func reuseIdentifier() -> String
  func setupModel(model: ModelType)
  var cell: UITableViewCell { get }
}

extension ReusableCell {
  static func reuseIdentifier() -> String {
    return String(describing: Self.self)
  }
  var cell: UITableViewCell {
    return self as UITableViewCell
  }
}

我的问题是,为什么我需要在UITableView扩展中执行此let t = model as! T.CellType.ModelType,而不是仅将模型传递给setupModel(model: ModelType)函数。

1 个答案:

答案 0 :(得分:1)

您需要将约束pipeline { agent none stages { stage('Build') { agent { docker { image 'python:3-alpine' } } steps { sh 'virtualenv venv --distribute' sh 'source venv/bin/activate ' sh 'pip install --user -r requirements.txt' sh 'python WebChecker.py' } post { always { junit 'output.xml' } } } } } 添加到CellType.ModelType == Self

CellViewModel