Lesson 006 – Classes and Structs

ClassAccording to Wikipedia, in Object-Oriented Programming (OOP) terms, a Class is “an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).” If you aren’t already familiar with OOP and Classes, that might sound like a lot of jibberish to you. Our goal in this lesson is to hopefully clear that up a little bit.

Introduction to Classes

A class is just a grouping of functionality that all has something in common. That can include variables and functions. In reality, we have already dealt with classes in Swift. For instance, String is a class. The String class has many methods on it that you can use to operate on the string. In this example, I declare a String variable called blogName and then I call the exposed member lowercaseString to return “swift ninja” in this case.

This makes sense. If you wanted to operate on a String, you wouldn’t want to have to look all over for how to work with it. You can find much of what you need in the String class. A class is just a blueprint. It doesn’t really exist. When I created blogName I created a specific instance of the class String. That is what an object is, a specific class that has been created and actually represents something concrete. Not the “idea” of some letters, the actual letters S w i f t N i n j a in this case.

Creating Our Own Classes

Programming wouldn’t be very fun if we were only stuck with the classes that we got from Swift “out of the box”. Let’s think of something that we can model in a class. I think that we can all picture and describe a car pretty easily. How can you describe a car? You can describe its color, number of doors, engine size, make, model, and model year. Those things are properties of a class. What car a car do? It can drive forward and backward, turn left, turn right, and stop. Those things are the functions. When you declare functions inside of a class, you call them methods, so those are the car’s methods. Let’s take a look at how we might accomplish making a Car class.

So here we use the keyword class and then give the class a name. In this case, I chose “Car”. I declared six variables just like we saw in Lesson 001 and then declared a method using the func keyword (crazy, I know!), just like we learned in Lesson 004. And that is all there is to very simple classes. All of those properties and that method aren’t floating around your code, they are all wrapped up in the idea of a car. But, a generic car isn’t really a thing. You don’t drive a “car”, you drive a 1977 Blue Ford Pinto, a specific instance of a car. You should already be pretty familiar with how we are going to create our object variable.

The Init Method

So, on the first line, we declare our instance of a car, an object we are calling myCar. After that, I set the six properties and then call the go() method. But really, you don’t get to actually MAKE a car and THEN decide what kinds of attributes it has. All of those things are decided as it is getting built. Fortunately, Swift has a way to handle that. You can provide details about your object as it is being created so it will be created to your liking. You do that with the init() method. Every class has one (it is assumed to be blank if you don’t type it). It is the init method that you are calling when you put the () in the var myCar = Car() line. Let’s see how we could use the init method to make our code a little cleaner and true to life.

There are a few special things about the init method. The first one is that you don’t specify a return type. You might be thinking, “Well, didn’t you say you could leave it off and Swift would assume it returned Void?” Yes, I absolutely said that. But in this case, init() isn’t returning Void, it is returning a Car. Another strange thing is that you can name the parameters the same thing as the properties in the class. The way you tell them apart is that you reference the properties as self.propertyName and the parameters just as parameterName. The third odd thing is that when we call the init() method while constructing the object from the class, we provide the name of the first parameter make:. If you remember, unless we provided an external name, Swift didn’t like it if you called the first parameter by name. But init methods? Anything for you, buddy!

Structs

So, that covers the very basics of classes. Next, let’s look at structs. In Swift, structs are basically exactly like classes. They can have methods, properties, and init methods. Let’s look at a simple struct example and then we can talk about how classes and structs differ.

You can see from the syntax that everything is exactly the same as a class. In fact, if I replace struct with class, this code would still work as I’ve typed it.

What’s the Difference?!?

The difference between a struct and a class is how they are handled in memory. Whenever I pass myCar around in my application, it is always pointing to the same exact car. Not one that looks like it… the SAME CAR. If I change something on one Car, every reference to the car is also changed at the same time. However, when using a struct, your program makes a separate copy every time. That way, you can change something in one place and it only affects that one place. Let me show you how that works.

Now let’s check a few things

That seems right. We only made one of each and assigned them to the other variables. What if we change a property on the original? What happens then?

So, I changed the color on carOne to “Black” and the model on planeOne to “757” and then printed out the values again. You can see that when I changed carOne to “Black”, all of the cars became black. However, when I changed planeOne to a “757”, only that one changed. That’s the main difference. Often, you may do entire coding projects and never even use a struct. They are typically used for very specific reasons. One of those reasons is to reduce “side effects”, intentional or not. If you write programs in a “functional” style, one of the tenets of Functional Programming is that you do not have side effects.

Conclusion

So, we just covered the very basics of classes and structs in this lesson. Honestly, this is one of those topics where it is easier to learn by doing. We’ll learn more about how classes work as we begin to make actual iOS applications. Hopefully, things will be a lot more clear as we use them in “real world” scenarios. Remember that all of our code is up on our GitHub repository.

Add Comment

Required fields are marked *. Your email address will not be published.