# Trouble adding AddItemViewControllerDelegate

**URL:** <https://forums.kodeco.com/t/trouble-adding-additemviewcontrollerdelegate/28327>\
**Category:** iOS Apprentice\
**Created:** [April 12, 2017, 2:33pm UTC](https://forums.kodeco.com/t/trouble-adding-additemviewcontrollerdelegate/28327 "2017-04-12T14:33:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdenlinger](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pdenlinger/32/81494_2.png) [@pdenlinger](https://forums.kodeco.com/u/pdenlinger)\
**Post date:** [April 12, 2017, 2:33pm UTC](https://forums.kodeco.com/t/trouble-adding-additemviewcontrollerdelegate/28327/1 "2017-04-12T14:33:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fusetech](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fusetech/32/97552_2.png) [@fusetech](https://forums.kodeco.com/u/fusetech)\
**Post date:** [April 12, 2017, 2:42pm UTC](https://forums.kodeco.com/t/trouble-adding-additemviewcontrollerdelegate/28327/2 "2017-04-12T14:42:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pdenlinger](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pdenlinger/32/81494_2.png) [@pdenlinger](https://forums.kodeco.com/u/pdenlinger)\
**Post date:** [April 13, 2017, 11:03pm UTC](https://forums.kodeco.com/t/trouble-adding-additemviewcontrollerdelegate/28327/3 "2017-04-13T23:03:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [April 14, 2017, 9:34am UTC](https://forums.kodeco.com/t/trouble-adding-additemviewcontrollerdelegate/28327/4 "2017-04-14T09:34:00Z")

</div>

> [@fusetech](#):
>
> 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)

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