Chapter 20 init

In chapter 20, we use override init in the ChecklistItem.swift, but in Checklist.swift, we use init.
What is the difference then?

Hi @feanor, init( ) is used to create a new instance of a specific type whereas using override will allow initialization for a subclass that the parent class already has.


Thank you very much!

why do we need initialization of the parent class? in this case, NSObject?

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

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

If you declare an init method in a subclass, you traditionally call the parent version somewhere in your method, in case the parent method does something important. For other methods it depends on whether your code is a complete replacement or not.

NSObject has an init method declared as init().

The reason you have to override it in the ChecklistItem class is that you are writing an init that looks just the same, declared as init().

The reason you don’t have to override in the Checklist class is that you are writing an init that looks different, declared as init(_ name: String, iconName: String = “No Icon”) .

For a method with the same signature (name and parameters), you have to override. For one with its own unique signature, you don’t.

Both of the new init methods call super.init(), as they should. I would bet the NSObject init() does something, and would not take the chance that it does nothing.