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)