Beginning Table Views · Creating a Model | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5995-beginning-table-views/lessons/14

What is the purpose of initializer?
In the ios tutorial about the bull’s eye app, it does not require that initializer.
If we use some model, that is mandatory?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

The initializer allows us to setup values for an object when the object is created. In this case, we create a few objects that will use to populate the data. I hope that helps!

Hi, could you please explain the required init ?(coder aDecoder: NSCoder) {

todoList = TodoList()

super .init(coder: aDecoder)

}

What is this and why do we need it?
This is called when this view controller is initialised from the storyboard. I don’t really understand it…
Is this an initialiser method that is called when the user is allowed to fill in a tableview ie Checklist (customise it) with his own list? So basically all table view would have this initialiser method?
Many Thanks in advance

This method is called when the storyboard creates an instance of the view controller, so that’s a good place to do some initialization. There are other places you can initialize your objects as well. And yes, a table view does have this initializer as well.

Thank you for your answer, Brian. Can I please ask you if you have this method NSCoder in the iOS APPRENTICE BOOK, particularly for this app? Is it included in the code ?

Brian,

Please, how is it possible that the same app in the book does not contain the below code anywhere? It is not in the book… Checklist app. I wanted to look up this part to read more about it…

required init?(coder aDecoder: NSCoder) {
super.init()
}

override init() {
super.init()
}

Hey there,

When we created these video tutorials, we started with the iOS Apprentice and ultimately the app evolved from there. This is why there is a difference. If you are curious about NSCoding, you can check out this tutorial over here:

https://www.raywenderlich.com/6733-nscoding-tutorial-for-ios-how-to-permanently-save-app-data

Feel free to shoot me any questions if you have them

I have a question when we create the variable “row0Item”. It is first created as a CheckListItem type. Then it is set as a base CheckListItem class through the initializer.

var row0Item: CheckListItem

required init?(coder aDecoder: NSCoder) {
    row0Item = CheckListItem()
    row0Item.name = "Take a jog"
    super.init(coder: aDecoder)
}

Why would we not just set it as a base CheckListItem class to begin with? i.e.

var row0Item = CheckListItem()

required init?(coder aDecoder: NSCoder) {
    row0Item.name = "Take a jog"
    super.init(coder: aDecoder)
}

Is this a best practice/matter of preference or is there a reason we would prefer to first declare its type then set it equal to a base class in the initializer?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Apple lays out the best practice in their Swift programming guide. Essentially, if the property will always start with the same initial value, then use a default property. You can find out more over here:

https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

Look under the section titled Default Property Values. I hope that helps!

That does help, thank you!

What does super.init(coder: aDecoder) do?

@ericsutton33 Please check out this article when you get a chance:

I hope it helps!

Why do we need row0Item.checked = !row0Item.checked and what does it do exactly? I thought that the code below would change the state of the check:

if indexPath.row == 0 {
if row0Item.checked {
cell.accessoryType = .none
} else {
cell.accessoryType = .checkmark
}

1 Like

I have the same question.
Could you please kindly explain why row0Item.checked = !row0Item.checked is needed in addition to the code below?

if indexPath.row == 0 {
if row0Item.checked {
cell.accessoryType = .none
} else {
cell.accessoryType = .checkmark
}

This code changes the checkmark data to point to the opposite of its current state.

row0Item.checked = !rowOItem.checked

This means that if the checked property is true, it is now false. If it’s false, it’s now true.

This code here:

if row0Item.checked {
    cell.accessoryType = .none
} else {
    cell.accessoryType = .checkmark
}

This updates the tableview based on the checked property. This code visually adds the checkmark.

Whenever you run into a question about what a certain line of code does, try deleting it and see the results.

In any case, I hope my explanation helps.

Hi Brian,

Thank you for the follow up.

Is it fair to say that row0Item.checked = !rowOItem.checked is related to tableview datasource and if row0Item.checked {
cell.accessoryType = .none
} else {
cell.accessoryType = .checkmark
}
is linked to the tableview delegate method?

Sincerely, Joey.

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]