Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Inheritance Inheritance and Initializers

taisuke araki
taisuke araki
2,524 Points

Why self.title = title doesn't work around 4:20?

Amit said "because we already have an initializer method in super class". But I think the the super class's init method hasn't been called anywhere.

Why does system consider the constant self.title is already set?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi taisuke araki,

In Swift and other languages alike, class inheritance only does so much when creating an instance of a child class that extends a parent class. In Swift, the compiler is always running which evaluates code in real-time. The reason we simply can't just call self.title is because the class Clothing doesn't have a property called title, we could try adding it but that would cause an error because it's already declared in the Product class.

Why does system consider the constant self.title is already set?

As mentioned above, Swift doesn't assume that self.title is set, it complains because you're attempting to set a value for a property that doesn't exist within Children therefore we first need to call super.init and pass our title through in order to set a value for it.

Think of the compiler as a safe guard, it's always looking out for us and ensuring that from a high level that our code is working to an extent but not entirely.

Hope that helps.