Group Group Group Group Group Group Group Group Group

Chapter 20 Page 870

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.

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.

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?

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.

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.

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

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()
}

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

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