# Checklists bonus exercise Chapter 19

**URL:** <https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242>\
**Category:** iOS Apprentice\
**Created:** [January 4, 2018, 5:24am UTC](https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242 "2018-01-04T05:24:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thunderken](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/thunderken/32/78895_2.png) [@thunderken](https://forums.kodeco.com/u/thunderken)\
**Post date:** [January 4, 2018, 5:24am UTC](https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242/1 "2018-01-04T05:24:16Z")

</div>

I was looking to see if I did this right. the exercise is in Chapter 19, page 430.

> **Exercise** : Give ChecklistItem an init(text:) method that is used instead of the parameter-less init(). Or how about an init(text:checked:) method?

Inside of **ChecklistItem.swift** I added these lines:

```
init(text: String, checked: Bool) {
    self.text = text
    self.checked = false
    super.init()
}

```

Inside of **ItemDetailViewController.swift** I changed the `done()` function:

```
@IBAction func done() {
    if let itemToEdit = itemToEdit {
        itemToEdit.text = textField.text!
        delegate?.itemDetailViewController(self, didFinishEditing: itemToEdit)
    } else {
        // Added this line
        let item = ChecklistItem(text: textField.text!, checked: false)
        
        // Replaced these lines
        // item.text = textField.text!
        // item.checked = false
    
    delegate?.itemDetailViewController(self, didFinishAdding: item)
    }

```

I guess that I could also pass the `false` parameter as default as well? So would this be correct?

```
init(text: String, checked: Bool = false) {
    self.text = text
    self.checked = checked
    super.init()
}

```

But this don’t work “use of unresolved identifier”:

```
 let item = ChecklistItem(text: textField.text!, checked: checked)

```

---

<div class="post-metadata">

**Author:** ![fahim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fahim/32/110943_2.png) [@fahim](https://forums.kodeco.com/u/fahim)\
**Post date:** [January 4, 2018, 6:03am UTC](https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242/2 "2018-01-04T06:03:29Z")

</div>

> [@thunderken](#):
>
> I was looking to see if I did this right.

The initial code looks fine to me and if it works when you run it, you know that it definitely works 🙂

> [@thunderken](#):
>
> I guess that I could also pass the false parameter as default as well? So would this be correct?

Yes, you can certainly have a default value for the parameter since that’s a standard Swift feature and the code you provided looks good to me. The reason you get an error for the following line:

`let item = ChecklistItem(text: textField.text!, checked: checked)`

Is probably because the `checked` variable that you pass to the initializer has not been defined anywhere. Plus, if you are initially creating an instance of `ChecklistItem`, wouldn’t you be using the default parameter (which is `false`) instead of passing a value? So that line should probably look like this:

`let item = ChecklistItem(text: textField.text!)`

---

<div class="post-metadata">

**Author:** ![thunderken](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/thunderken/32/78895_2.png) [@thunderken](https://forums.kodeco.com/u/thunderken)\
**Post date:** [January 4, 2018, 6:28am UTC](https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242/3 "2018-01-04T06:28:08Z")

</div>

@fahim

I think that it works as expected when setting the default in the `init` and when using:

```
let item = ChecklistItem(text: textField.text!)

```

Thanks for the info.

---

<div class="post-metadata">

**Author:** ![fahim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fahim/32/110943_2.png) [@fahim](https://forums.kodeco.com/u/fahim)\
**Post date:** [January 4, 2018, 7:37am UTC](https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242/4 "2018-01-04T07:37:04Z")

</div>

> [@thunderken](#):
>
> I think that it works as expected when setting the default in the init and when using:

And if you want to initialize with the `checked` parameter set to `true`, then you’d call the method like this:

`let item = ChecklistItem(text: textField.text!, checked: true)`

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [June 19, 2018, 9:24pm UTC](https://forums.kodeco.com/t/checklists-bonus-exercise-chapter-19/38242/5 "2018-06-19T21:24:18Z")

</div>

Closed on June 19, 2018, 9:24pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
