Trouble adding AddItemViewControllerDelegate

I am having some trouble with adding the AddItemViewControllerDelegate on page 103 of Checklists. I have pasted in all the code from ChecklistViewController below, and have included the error messages in comments after the relevant parts of the code:

import UIKit

class ChecklistViewController: UITableViewController, AddItemViewControllerDelegate { //“ChecklistViewController” does not conform to protocol “AddItemViewControllerDelegate”

var items: [ChecklistItem]

required init?(coder aDecoder: NSCoder) {
    items = [ChecklistItem]()
    
    let row0item = ChecklistItem()
    row0item.text = "Walk the dog"
    row0item.checked = false
    items.append(row0item)
    
    let row1item = ChecklistItem()
    row1item.text = "Brush my teeth"
    row1item.checked = true
    items.append(row1item)
    
    let row2item = ChecklistItem()
    row2item.text = "Learn iOS development"
    row2item.checked = true
    items.append(row2item)
    
    let row3item = ChecklistItem()
    row3item.text = "Soccer practice"
    row3item.checked = false
    items.append(row3item)
    
    let row4item = ChecklistItem()
    row4item.text = "Eat ice cream"
    row4item.checked = true
    items.append(row4item)
    
    super.init(coder: aDecoder)
}

@IBAction func addItem() {
    let newRowIndex = items.count
    
    let item = ChecklistItem()
    item.text = "I am a new row"
    item.checked = false
    items.append(item)
    
    let indexPath = IndexPath(row: newRowIndex, section: 0)
    let indexPaths = [indexPath]
    tableView.insertRows(at: indexPaths, with: .automatic)
    
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath)
    
    let item = items[indexPath.row]
    
    let label = cell.viewWithTag(1000) as! UILabel
    label.text = item.text
    
    configureText(for: cell, with: item)
    configureCheckmark(for:cell, with: item)
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if let cell = tableView.cellForRow(at: indexPath) {
        let item = items[indexPath.row]
        item.toggleChecked()
        configureCheckmark(for: cell, with: item)
        
        configureCheckmark(for: cell, with: item)
    }
tableView.deselectRow(at: indexPath, animated: true)
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath:IndexPath){
    
    items.remove(at: indexPath.row)
    
    let indexPaths = [indexPath]
    tableView.deleteRows(at: indexPaths, with: .automatic)
    }
}
func addItemViewControllerDidCancel(_ controller: AddItemViewController) {
    dismiss(animated:true, completion: nil) // use of unresolved identifier "dismiss"
}
func addItemViewController(_ controller: AddItemViewController, didFinishAdding item: ChecklistItem) {
    dismiss(animated:true, completion: nil) // use of unresolved identifier "dismiss"
}
func configureCheckmark(for cell: UITableViewCell, with item: ChecklistItem) {
    
    if item.checked {
        cell.accessoryType = .checkmark
    } else {
        cell.accessoryType = .none
    }

    
}

func configureText(for cell: UITableViewCell, with item: ChecklistItem) {
    let label = cell.viewWithTag(1000) as! UILabel
    label.text = item.text
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // override can only be specified on class members
    if segue.identifier == "AddItem" {
    let navigationController = segue.destination as! UINavigationController
    let controller = navigationController.topViewController as! AddItemViewController
    controller.delegate = self // use of unresolved identifier 'self'
}

}
}

Your curly braces have gotten out of sync. All of these functions are intended to be a part of ChecklistViewController, but you have “closed” that class after tableView.commitEditingStyle() around line 98.

addItemVCDidcancel, addItemVCDidFinishAdding, configureCheckmark, configureText and prepareForSegue are all on the “outside” of the class they are intended for.

Try this: Cmd + A, then Control + I. This will select everything in your file, and then re-indent it. You will see how about half way down the file, your function declarations are no longer indented (because they are actually being placed outside of the class)

The quickest solution is to delete the extraneous “}” that is around 98.

Hope this helps

1 Like

Thank you for pointing out the problem.
I went back and just coded everything up to there again; I find doing it all over helps me to remember, as the more frequently I do it, the more I retain.
Now I know I need to pay special attention to braces!
Thank you.

+1 for this tip, so useful when you are lost in the bracesUniverse :smile: