The code below is to show the name of the cells in UItableview.
override func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell
cell.textLabel!.text = "Spring \(indexPath.row + 1)"
return cell
}
There is a compiling error and Xcode suggests me to change the line 'as' into 'as!'-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
as! UITableViewCell
May someone explains what is downcast and why do we need to downcast in this case?
Because -dequeueReusableCellWithIdentifier:
returns AnyObject
(id
in Objective-C). Swift needs you to force downcast because AnyObject
can be any object and by force casting, you're telling the compiler to disregard the current type and force cast to a more specific object down the inheritance line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With