Previous episode: 40. Classes
Next episode: 42. Conclusion
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Hey, it's time for some practice with classes. Now, you can find the challenge in the 08-Challenge-Classes page of the playground you've been using, or you can download a new one from the resources for this video. Open it up, give everything a try on your own, but then keep watching and you can compare your work to my work. Good luck. (upbeat music) Okay, here's our challenge. So, convert student struct below to a class you'll need to use a class keyword, create an initializer fix up the mutating method and make chris a constant. All right, well, this code looks familiar. It's the students structure from the structure's episode. So the challenge is to transform this struct into a class. So, first, I'll replace the struct keyword with class. (instructor typing) Now, I'm seeing three errors but two of them can be solved by writing an initializer. First use the keyword in it. (instructor typing) Followed by a parameter list with a parameter for each property. (instructor typing) Now, you could also set default values for these parameters just like you would with function. I'm gonna make pet default to nil. (instructor typing) And to finish up the initializer, I'll set the property values using the matching parameters with self. (instructor typing) Now, if I want to make a new student without a pet, I can use this initializer that only needs two arguments. (instructor typing) Next, I just need to get rid of mutating. As the error says, it doesn't work with classes. (instructor typing) So all the errors are gone now but there's one more thing I can do. Because this is a class, I can make chris a constant (instructor typing) and this earn extra credit method will still successfully change his grade.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.