uitableview - Swift - what is downcasting? why do i need to downcast the cell in tableview? -


the code below show name of cells in uitableview.

 override func tableview(tableview: uitableview,         cellforrowatindexpath         indexpath: nsindexpath) -> uitableviewcell {          let cell = tableview.dequeuereusablecellwithidentifier("cell")             uitableviewcell          cell.textlabel!.text = "spring \(indexpath.row + 1)"          return cell } 

there compiling error , xcode suggests me change line 'as' 'as!'-

    let cell = tableview.dequeuereusablecellwithidentifier("cell")         as! uitableviewcell 

may explains downcast , why need downcast in case?

ok..lets make simple.

on line:

let cell = tableview.dequeuereusablecellwithidentifier("cell") 

you accessing method dequeuereusablecellwithidentifier of tableview, if @ documentation can see return type anyobject?.

now suppose want access textlabel property

cell.textlabel!.text = "spring \(indexpath.row + 1)" 

you cannot it..because there no textlabel on anyobject type.

so, downcasting force compiler more specific class, uitableviewcell, below because later trying access text label property:

let cell = tableview.dequeuereusablecellwithidentifier("cell") as! uitableviewcell 

Comments