Reputation: 13
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
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
Reputation: 5694
You need to unwrap label from Optional before use it.
cell.textLabel?.text = ToDoItems[indexPath.row]
Upvotes: 2