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 trialtaisuke araki
2,524 PointsWhy 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
26,676 PointsHi 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.
taisuke araki
2,524 Pointstaisuke araki
2,524 PointsThank you! Chris Upjohn .
I understand!