Classes and Objects in Swift

Hi! My name is Ashank, and I am one of the new writers here. This is the first post for my new series called Beginner’s Guide to Swift. With that being said, let’s get started!

This first post is about Classes and Objects within Swift. Prior knowledge of classes and objects is not needed to follow along. Even if you are unsure about what these terms are and how they work, do not worry. This post should help clear up any confusion you may have about classes and objects.

What Are Classes and Objects?

Simply put, classes are blueprints for something you want to have in your program. Let’s take an example. Suppose we want to have a car in our program. The class, or the blueprint, would look something like this:


 Credits To:

This blueprint has all the actions a car can do and all the properties a car has, such as color, wheel type, etc. that a car has. After writing this class, we can refer to this blueprint to create an actual car that can be used!



Credits To:

This is an actual car, or object, that has been created using the blueprint of the car! It can do all the actions and has all of the properties that we have written in the blueprint!

With this basic knowledge of classes and objects, we are going to make a Person class and use it to make some objects! Essentially, we will write our own blueprint of a person, with all of the actions and attributes that a person should have. Then, we will make our object using the blueprint, and finally, we will use our object to do whatever we want! Let’s take it to Xcode!

Writing Classes

If you do not have it already, download Xcode from the App Store on your Mac. Once you start up the app, you should see this screen:

Screen Shot 2015-08-27 at 7.51.07 PM

For now, we are only delving into Swift and not iOS applications using Swift, so press “Get Started With A Playground.” You can name the file anything you want, but for now, we are just going to keep it the default “MyPlayground” name. Make sure the Platform option shows iOS.


Screen Shot 2015-08-27 at 7.55.51 PM



Press next and save the file on your desktop. Upon opening the playground, you should see something like this:


Screen Shot 2015-08-27 at 8.00.40 PM


The left side of the playground is where you will be writing all of your code, and the right side of the playground displays the actions called by your code. Just to review, “var” is a keyword for “variable” which essentially holds any value. “str” is the name of the variable, and it holds a value of “Hello, playground”. Any text in Swift surrounded by quotes refers to a String data type, or essentially a string of characters. On a side note, the “import” statement can be deleted at the top of the playground.

With that all out of the way, let’s create our first class! The class name will be Person, and we will define variables for the first name, last name, age, and gender, all of which are properties of a normal person. Let’s remove the “Hello, playground” statement and replace it with a class.


Screen Shot 2015-08-27 at 8.53.12 PM


That’s all there is to creating a class! As you can see, the keyword for creating a class is “class” (all keywords are highlighted in purple) and the name of our class is “Person.” Now, let’s add all the properties to this Person blueprint. We will have a String for the first name, a String for the last name, an Integer for the age, and a String for the gender.


Screen Shot 2015-08-27 at 8.59.48 PM


Let me explain what we are doing here. We have the keyword “var” which instantiates a variable with the name of a property of a person. The data type has to follow the variable name with a colon to let Swift know the type of variable you want. Since names and genders are strings of characters, we have “String” after each one of those variables. For age, however, we have “Int,” which is short for Integer, or any number that is not a fraction. So this is how you define properties of a class.

Now if you notice to the left of the class statement, there is a red stop sign. This indicates that Swift is recognizing an error. If you click on the stop sign, some text will appear to the right.

Screen Shot 2015-08-27 at 9.03.56 PM

The Swift Compiler is giving an error here because the class we have defined has no initializer. In other words, we do not have any way to create objects using this class! To create an object of this class, we will need an initializer function in order to bring this object to life! Let’s write this function.


Screen Shot 2015-08-27 at 9.17.38 PM


This initializer has added a lot of code to our program. Let’s go step by step into what it actually means.

The first thing we do is write the “init” keyword, short for initializer. Within the parenthesis that follow, add the properties in the same fashion that you defined them. Next, the code within the brackets consists of the “self” keyword. The “self.firstName” or any variable with “self” before it expresses that you are referring to the properties of the class. The properties in the parenthesis are the properties that you assign to the object you create. This will make more sense later when we create an object of the Person class.

To finish our class, let’s define a function called startTalking, which will display some text when the function is called.


Screen Shot 2015-08-27 at 9.39.28 PM

Once again, let’s go step by step through the process of creating our startTalking function. The keyword for defining a function is “func,” as you can see below. The name of our function comes after the keyword, which is followed by a set of parenthesis (which we will discuss later). At the end of a statement is a “-> String.” The arrow followed by “String” shows that the function will be returning a method to us that we will be able to see in the output on the right. As you can see, there is a “return” keyword and a string: “I just started to talk!” If we have written this function correctly, we should see that string in the output. Once again, all of this will start to fall in place after we create the objects.

Now that we have our basic class set up, let’s move on to creating the objects using our created blueprint of a Person.


Creating Objects of a Class

We will now use the initializer and startTalking method that we created in our class and test them out and see the outputs. Our person will have a first and last name of “Bob,” a male gender, and an age of 20. Here is how we make an object.


Screen Shot 2015-08-27 at 9.52.21 PM

At the bottom of this screenshot, you can see my new variable called “personOne”. To tell Swift that “personOne” is part of the Person class, we state “Person” after the equal sign. The next part should seem familiar. As you can see, we are calling the “init” function in this statement because we are telling Swift what the first and last name, the gender, and the age of “personOne” is. By writing this statement, we have successfully created an object of type Person! At the right of the picture you can see that Swift has successfully recognized all of our properties and has printed them out.

The next step is to call the startTalking() function that we defined in the class. Well, that is only one line as well!


Screen Shot 2015-08-27 at 9.58.57 PM


To call the function, all you have to do is type “personOne,” add a period symbol to indicate that you are calling a method, and then type the name of the method! Since we are returning “I just started to talk” in the method defined in the class, the console prints out “I just started to talk!” It’s as simple as that!

We can also do several other things with our new objects. For example, if we wanted to change the age or the name of our object, all we have to do is this:


We do the same thing as we did while calling a method, except that we change the method name to the property name! In this case, I changed personOne’s name from Bob to Sam. On the right, you can see that this statement has been successful because it changed firstName to Sam. You can change any of personOne’s properties this way!

Another cool thing we can do is that if we wanted to create another object of type Person who is equally identical to “personOne,” all we have to do is define another variable and equate it to “personOne”!


Screen Shot 2015-08-27 at 10.06.51 PM

This was a very basic introduction to classes, functions, and objects, but this is one of the core features of all objective based programming. Try to make your own classes at home. Be creative! The more you get a grip on classes, the better you will be able to do in iOS programming. Hope I have helped, as there will be more to come! Please feel free to leave comments and suggestions for the next tutorial! Goodbye for now!