Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 5: Functions & Named Types

40. Classes

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 39. Challenge: Structures Next episode: 41. Challenge: Classes

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.

Learn more Already a subscriber? Sign in.

Notes: 40. Classes

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Classes are a lot like structures. They have properties and methods and initializers, but classes are reference types, instead of value types. In these exercises, you'll get an idea of how that affects your code. So in this episode, you're going to cover the basics of classes and Swift . And you'll do that by starting with a structure, which you should have a good handle on by now. And then you'll convert it to a class and compare the results. At the top of this playground page, I already have a simple actor structure set up for you. It has two properties, a constant string to represent the actor's name and a variable array of strings to act as their filmography. For this exercise, that's just going to be an array of film titles, and that's all we need to represent an actor like Zoe Saldana. I'll start her filmography off with Guardians of the Galaxy. So you may remember from the earlier episode on structures, if I want to change any of Zoe's properties, like adding to her filmography, I'll have to represent her with a variable, not a constant. So I'll call that variable GOGT star. So now I can add Avatar to her filmography. Okay. So another variable name that describes Zoe is Star Trek star. So I'll assign Guardians of the Galaxy star to Star Trek star to represent that equivalents. There. Now I'll just add Star Trek to her filmography. Now all of these films so far are just the first entries in franchises. So let's add a method that will allow an actor to sign on for franchise sequels, using the name of the franchise. So I'm going to have to mark this as mutating because it's going to modify the filmography. And so the way it's going to modify the filmography, is going to append upcoming sequel with the franchise name. Okay. Now Zoe is an Avatar star. So let's make a third variable and we'll assign it Star Trek stars value. And then because at this point Zoe's filmography has made up completely of movies with the same names as their franchises, you can loop through Avatar stars filmography, and sign her on for sequel to each one. Okay, so let's check out Avatar stars filmography. Okay, well Zoe's real filmography is a lot longer than that, but that's enough for this exercise. Okay, and now, because Star Trek star and GOTG star are also Zoe Saldana, they should have the same filmography, right? Oh, wow. Wrong. Okay. What about Guardians of the Galaxy filmography? Wow, even more wrong. Okay. So we are going to get this sorted out. I promise, but let's take a look at why you're seeing this behavior. So as you've learned, structures are value types, which means that everything their instances contain is copied on assignment. In this example, imagine that you create person with an initializer providing the values Ray and Wenderlich, and then you assign person to another variable instructor. And that's kind of like what you're doing in the exercise. You give the same person, different variable names. But the two instances you have now are totally independent. If you were to change person's name to Bob Wenderlich, instructor would still be Ray Wenderlich, but what you probably want, instead, it looks more like this. Both person and instructor should reference the same instance of a person type and as you see in the diagram, that can be done with a class instance. That's also known as an object. So let's convert your actor value type to be a reference type, instead. Now the first change is use the keyword class instead of struct. Now the errors that will appear are going to help us complete this transformation into a class. The first one says, actor doesn't have an initializer, which is true. Classes don't get an automatically generated initializers like structures do, but that's not a big deal though. So initializers are fairly similar to methods in syntax, and you've got a good handle on those. So we'll start a new initializer with a keyword init. And then add a parameter list that includes both properties and their types. So we wrapped it up with a pair of curly braces now. So within the body of the initializer, you need to assign the parameters to the classes properties, and you do that by saying equals name. Now the self here is referring to the specific instance of this class that will be created by the initializer. Let's do the same thing for filmography. And then we're told somewhat unclearly that the keyword mutating is not used in classes. So you can just delete it. Done. You're still performing a mutation with that method, but only to the filmography. Remember, with the structure, when you mutate any of its properties, you're actually making an entirely new structure just based on the original, but that's not true with classes. You still can't mutate constant properties of a class instance, like an actor's name, but you can change anything marked with the VAR and Swift won't consider that you're working with a new object. It's still the old object, but with new values. And that applies even if your object is declared as a constant. Let's try that. So now that you know that you can, let's change the first two of these star variables to be constants. Now we've got an actor class and we're definitely making nutations to the class instances, but they're all to the same actors filmography now. So to me, Avatar star, Star Trek star, and GOTG star, should all be referencing the same Zoe Saldana. So for modeling this particular actor type, I think a class is the way to go. Hey, let's sum up what you've learned about structures and classes in the last few episodes. So structures are value types and classes are reference types. An instance of a structure is conceptually a value, class instances or objects with identity. Now structures copy their values when used in a new place, but classes share their data. And structures are completely immutable when they're declared as constants, while class properties remain mutable. So as you get more experienced, I know you're going to have a better grasp on classes and structures and their different capabilities. So after you've completed the next course in this learning path, your second iOS and Swift UI app, will have another Swift course waiting for you. And that's going to take a much closer look at not only structure and classes, but one more name type called an enumeration. But before any of that, I've got one final challenge for you in the next video. I'll see you there.