May3
May3

Reputation: 13

'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
}

Upvotes: 0

Views: 719

Answers (2)

Patrick Lynch
Patrick Lynch

Reputation: 2782

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]

Upvotes: 4

Nikita Leonov
Nikita Leonov

Reputation: 5694

You need to unwrap label from Optional before use it.

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

Upvotes: 2

Related Questions