'UILabel?' does not have a member named 'text'

This is the code for a To Do list app I am trying to create:

import UIKit

class MyTableViewController: UITableViewController {

var ToDoItems = ["Buy Groceries", "Pickup Laundry", "Wash Car", "Return Library Books", "Complete Assignment"]

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "ToDoCell"
    let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as UITableViewCell

    // Configure the cell...
    cell.textLabel.text = ToDoItems[indexPath.row] //This is where the issue is

    return cell
}

Answers


The safe way to do it is with an optional binding:

if let label = cell.textLabel {
    label.text = ToDoItems[indexPath.row]
}

Or you could use optional chaining:

cell.textLabel?.text = ToDoItems[indexPath.row]

You need to unwrap label from Optional before use it.

cell.textLabel?.text = ToDoItems[indexPath.row]

Need Your Help

Delete specific image from loader

image function actionscript-3 bitmap removechild

I have three images all from the same loader on screen. I need to delete a specific (targeted) bitmap once clicked. I have an onClick function below, any help is greatly appreciated. Thanks!