Initializers | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/40

Hi Catie,

I’m a bit confused on how I have this compiler error while in the tutorial, XCode shows that you do not have this error.

I’m assuming it’s because in the designated init of the StudentAthlete class, there is no call to the designated init of its direct superclass, Student. Instead, it calls out to the designated init of 2 superclasses above it, the Person class.

Can you please elaborate on why I’m confronting this error but there’s no compiler error in the tutorial?

Hiya! I think the problem you’re seeing is due to the override keyword in front of StudentAthlete's initializer. Because you have a sports parameter in that parameter list, it isn’t actually overriding anything. None of the parent classes have sports parameters in their initializers.

Hi Catie,

About the convenience initializer for transfer in class StudentAthlete, shouldn’t that initializer call it’s super (with a transfer as parameter) in class Student instead of being a convenience initializer?

Reasonning being that you might want to do specific things when dealing with a transfer at the Student class level that you would end up not executing by being a convenience initializer and forced to call self.init? Basically what you end up doing with the grades copy code that you have to duplicate…

Hi! Thanks for your question :]

Yes, if you wanted to have logic about transferring grades that applies to all students, it would make sense to handle at the Student level. And, yep, right now this example’s grade transferring logic matches that description! You could do that by adjusting the initializers, like you suggest. In that case, I think you’d need to add a required init for Student, and manually transfer over the sports for StudentAthlete.

This example is pretty stripped down, so you can think through a lot of hypothetical scenarios, here. A fun exercise might be to make up new requirements and try to implement them. For example, if the grades transferring logic was complicated, maybe you’d break that out into its own method. Maybe you need special logic for transferring StudentAthlete grades that doesn’t apply to all students and you need to override Student's method. Maybe you can’t transfer athletes that have 3 failing grades (you can read about failable initializers here!)