az2902
az2902

Reputation: 428

What is this line of code doing?

This is from public Eureka project on Github. I'm trying to understand the source code (and Swift generics). I'm stuck on the very last line here on this snippet. What is the last line doing?

open class Row<Cell: CellType>: RowOf<Cell.Value>, TypedRowType where Cell: BaseCell {

    /// Responsible for creating the cell for this row.
    public var cellProvider = CellProvider<Cell>()

    /// The type of the cell associated to this row.
    public let cellType: Cell.Type! = Cell.self //what is this line doing?

Upvotes: 0

Views: 86

Answers (2)

Alberto Cantallops
Alberto Cantallops

Reputation: 309

When you call .self as a static attribute of a class retrieves the type of that class.

So if Row<CustomCell> the cellType will have CustomCell.Type assigned.

Upvotes: 2

Rob Napier
Rob Napier

Reputation: 299265

It's assigning the property cellType to the type of Cell that this generic was specialized over. So if this is a Row<PersonCell>, cellType is PersonCell.

The ! here is almost certainly unnecessary, and can create some headaches using this in Swift 3.

Upvotes: 5

Related Questions