Intro to OOP in Swift: Classes

Photo by Michael Dziedzic on Unsplash

Object Oriented Programming (OOP) is a programming model built around objects and data rather than functions. The model is built on the idea of encapsulation; that each class is responsible for their area of functionality and only that area. The encapsulation ensures that the code is adaptable to change, understandable, and scalable.

Before we begin making objects, it is important to understand what a class is. A class is one of the fundamental concepts in OOP and it acts as a blueprint for the creation of certain objects. A class holds data in attributes, and functionality in methods. For example, if we had an application which required us to keep track of various cars in a parking lot, we would require a way to store information about each car. This is where the “blueprint” or class comes into play.

Before delving into classes, open a Swift playground to follow along with the code.

In Swift class definition begins with the keyword classfollowed by the name of the class. Once the class container is created we can begin defining the various attributes and properties of the class. Similar to defining variables in a playground or script, the var keyword followed by the variable name and :Type

As you can see our Car class now has some outlined basic properties that can hold information for our objects, but the class itself does not hold any data. We have to instantiate an instance of this class, creating it in memory so it could hold values we can access later. Creating an object in Swift can be done quite easily. The code below creates an empty car object and passes it some data using the . notation.

We have our first car object! But it can be quite tedious to individually assign each property line by line every time an object is created, which is why it is important that classes have constructors.

A constructor is a subroutine that initialises the object, and it can be customised to take parameters and assign values. An object cannot have no values, which is why default values need to be provided, or a constructor be created. In our current class, we used default values for all of our properties when defining our class. We can improve this implementation by adding the init method to our class. This method takes in parameters when creating the object, and assigns the values respectively.

Now that we have a constructor, creating an instance of an object becomes a lot simpler.

To finish off our basic Car class, let’s add a unique method specific to our parking lot application that can print the amount of money a car owes if they have not yet paid.

Now we can call this function on the objects we have created and they can now perform the calculations for themselves.

The purpose of this will be more clear when we begin developing the Parking Lot class, which will manage and keep track of all the cars. Having individual methods within the Car class will allow the Parking Lot to simply have to ‘ask’ the individual car how much they owe instead of calculating it. This encapsulates the data of the Car to the Car class so if any object were to need information about a Car, they could find it within the Car object.

The concepts will become more complex as we progress through OOP in Swift, but hopefully the basics of classes and objects are clear so we can begin creating scalable and understandable code :)

11th Grade Student with a passion for Basketball, Gaming and Technology