如何投射到一种类型的T?

时间:2015-10-15 00:26:56

标签: ios swift generics

鉴于一些协议:

protocol CellConfigurable {
    typealias CellType

    func configureCell(type: CellType)
}

具体课程:

class SomeCellConfigClass: CellConfigurable {
   typealias CellType = SomeUITableViewCellSubclass

   func configureCell(type: CellType) {
      // do something    
   }
}

你如何将一个物体投射到T? e.g。

let cell = // dequeue a cell
let cellConfigObj = SomeCellConfigClass()
// can't do the following because cell is of type UITableViewCell
// and it expects a type of CellType
// someObj.configureCell(cell)

// So, we need to cast cell to CellType...but how? The following doesn't work
if let cell = cell as? someObj.CellType {
  someObj.configureCell(cell)
}

0 个答案:

没有答案