iOS Lesson 12 – Classes and Objects in Swift

We are going to cover the basics of Object-oriented programming principles in swift i.e Classes and Objects in Swift. You probably would’ve heard many times about classes and objects before but let’s see how they actually work.

Classes and Objects in Swift

First of all, Open up the playground and click on “Getting started with a playground”

We are going to call this one “OOP_Classes_Objects”. Delete the “hello, playground” line.

Object-oriented programming:

Object-oriented programming is the most popular form of programming. There are some new paradigms as well like functional programming or protocol-oriented programming. In Object-oriented programming, you just create blueprints of the stuff that you want to use later on and create multiple copies of it when you need it.

The perfect example of object-oriented programming in real life is “Human”. Its just like every new human being is made from a skeleton that is identical to everyone. “Skeleton is always the same but the color, muscles, strength etc. are different ”. The same thing goes with Object-oriented programming.


  • Blueprints (Classes) – Skeleton
  • Copies (Object) – Humans (Male, Female etc.)


Let’s move forward and use those examples right here.

When we want to make some objects (Humans) we would define some properties of the class. In our case a human has strength, the number of hands, Number of legs.

He also poses some powers like he can walk, run, Jump etc.


We can define a class using the keyword “Class” followed by class name and braces {} to define its body. Every property and method(abilities) are defined inside the class.


Classes and Objects in Swift


Here, we have defined three variables for properties of the skeleton. Two methods to define its abilities i.e walk, Run to show what it can do.

Just like human, class has properties i.e legs, Height, Hands and eye. It also has functions like Walk() and Run(). Now, what you really want to do to understand object-oriented programming is visualize the world around you.

Now, what we have done here is that we have defined our class but if we want to create an instance of it we can just write the Let keyword to make its copy.


let Male = Skeleton()

let female = Skeleton()

Here we have defined two humans using one skeleton class. Just like a female, Male have the same properties and the abilities. both can run and walk. Both have the same number of legs, hands and eye. We can call their methods as well. We can access the properties and methods of both instances by using the dot operator after the instance name.


Objects in Swift



So in this knowledge bit, we have covered Object-oriented programming principle such as Classes and Objects in Swift. We are going to use these classes and objects all the times. Earlier, We have seen few examples how we can create copies of those classes named as objects and how we can access their properties and methods. You can get all the code from here.

Please Like, share and subscribe for more!


You can have a look at our whole IOS series as well.

Click to comment

Leave a Reply

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

Most Popular

To Top