Group Group Group Group Group Group Group Group Group

Chapter 20 Page 870

#1

In chapter 20 we add an init method for ChecklistItem. Why is it that it needs an override? We have an init in Checklist and that one doesn’t need an override.

#2

Please note what the book says on Page 464: " Now, switch back to ChecklistItem.swift and add an init() method to set up the unique ID" - and that is the explanation :slight_smile: My apologies if it was not clear enough, but you want the unique ID to be set up when the ChecklistItem is initialized so that each ChecklistItem instance has a unique ID. So we override init to ensure that this happens.

#3

I understand that, but I thought we only use override methods when we create a method with the same name after we inherit a class?

#4

O never mind, I think I understand now. Since we are calling the .nextCheckListItemID() in the DataModel class, we have to override the init method that would come from the DataModel class.

#5

No, that’s not it. You don’t have to override a method in a class just because you are calling the method :slight_smile: But do look at how ChecklistItem is declared. It inherits from NSObject and NSObject has its own init method and that’s what you’re overriding.

#6

Ahhh, perfect. I didn’t know that about NSObject. Thank you!

#7

The Checklist object is inheriting from the NSObject as well, how come that doesn’t need overriding? in fact, it is written this way:

init(name: String, iconName: String = “No Icon”){
self.name = name
self.iconName = iconName
super.init()
}