Swift Classes and Objects


Like many other programming languages, Swift is also an object-oriented programming language and also keeps its procedural nature. That means, like other OOP languages, Swift also has the concept of creating classes and objects.

Classes in Swift

A Swift class is the blueprint of an object. You need to define the class only once, but you can create multiple objects of that same class.

Classes in Swift are similar to structures and can be used in the same way.

You can think of a class as the sketch of a house. The sketch can be used to build a real hose. Another interesting thing is that the same sketch can be used to build multiple houses of that kind.

Just like we can build multiple houses from the same sketch, in the same way you can create multiple objects from the same class.

How to define a class in Swift?

In Swift, you can define a class using the class keyword.

Below is the syntax for creating a class:

class className {
// Defination of Class. Contains properties anf methods
}

It is a good practice to use the first letter uppercase for the class name. Inside the class, we can define variables and functions that will be known as properties and methods.

Now let’s see a simple example of defining a class:

class Student {
    var firstName = "Christopher"
    var lastName = "Daniel"
    var age = 14
}

In the above examples, firstName, lastName and age are variables inside the class Student. These variables are the properties of the class because variables and constants are called properties if these are inside a class.

Objects in Swift

An instance of a class is known as an object.

You can take the example of the real world. We already discussed that a sketch of a house is the class, but when we build a real house from that sketch, then it will be an object. As we have already mentioned before, you can create many objects from the same class just like you can build many houses from the same sketch.

Below is the syntax of creating an object of a class:

var objectName = ClassName()

Now, below is an example of how we can create an object from our class:

// Create a class
class Student {
    var firstName = "Christopher"
    var lastName = "Daniel"
    var age = 14
}

// Create an object of our class
var student1 = Student()

In the example above, the student1 is the object of the class Student.

Accessing properties of the class using object

We can easily access the properties of our class through the object. To access the properties, we must have to create at least an object of the class.

Below is some example of accessing properties of using the object student1 that we created before:

// Access firstName property
student1.firstName

// Access lastName property
student1.lastName

// Access the age property
student1.age

You just need to use the object name followed by the property name after a dot (.) to access the property.

Now below is the Swift program where we are accessing properties of our class and printing the values of the properties:

// Create a class
class Student {
    var firstName = "Christopher"
    var lastName = "Daniel"
    var age = 14
}

// Create an object of our class
var student1 = Student()

// Access and print properties
print(student1.firstName)
print(student1.lastName)
print(student1.age)

The output of the above program:

Christopher
Daniel
14

In our example, we have first defined the class Student and then create an object student1.

Modify value of properties

We can change properties value of a class through the object. To modify the value of a property you just have to assign a new value to that property.

For example, to change the age of the student, all you need to do is just assign a new value as you can see below:

student1.age = 15

Now see the example below where we are modifying all the values of the properties of a class using its object:

// Create a class
class Student {
    var firstName = "Christopher"
    var lastName = "Daniel"
    var age = 14
}

// Create an object of our class
var student1 = Student()

// Print properties value before modifying
print(student1.firstName)
print(student1.lastName)
print(student1.age)

// Modify properties value
student1.firstName = "Maria"
student1.lastName = "Hernandez"
student1.age = 15

// Print properties value after modifying
print(student1.firstName)
print(student1.lastName)
print(student1.age)

Below is the output of the above program:

Christopher
Daniel
14
Maria
Hernandez
15

In the above example, first, we have printed the value of properties before modifying them, then we print them again after modifying the properties.

In the output, you can see both the property values before and after modifying them.

Multiple objects of the same class

As I have mentioned in this article, you can create multiple objects of one class. Now you are going to see the example.

First, let’s see the syntax:

var object1 = ClassName()
var object2 = ClassName()

In the above syntax, we have created two different objects. But these two objects are defined from the same class.

You can create as many objects as you want from the same class. Actually, it is a special feature that is so useful in app development for developers.

To create multiple objects we have to create multiple variables with different variable names. We can also use constants instead of variables to create multiple objects.

Now see the example below where we are creating multiple objects of the same class:

class Employee {
    var salary = 25000.00
}

// Creating object
var employee1 = Employee()
employee1.salary = 32000.00

// Creating another object of same class
var employee2 = Employee()
employee2.salary = 41000.00

// print property of the first object
print(employee1.salary)

// Print property of the second object
print(employee2.salary)

Output:

32000.0
41000.0

Function inside a Swift class (Methods)

We can define functions inside a class. The functions we define in a Swift class are known as the methods.

We can also access the functions inside the class using the object.

Below is shown an example of defining a function inside the class and accessing it through the object:

class CalculateSum {
    var num1 = 3
    var num2 = 4
    func sum() {
        let sum = num1 + num2
        print(sum)
    }
}

var mySum = CalculateSum()

mySum.sum()

Output:

7

Initialize properties in Swift class

Till now, we have assigned property values directly inside the class as you can see below:

class Student {
    var firstName = "Christopher"
}

But we can also initialize the properties instead of declaring values inside the function. We can assign the value later during creating the object. using the initializer function, we can initialize property.

To initialize a property in the Swift class, the init keyword is used. We can assign the value of the property during creating an object. The value will be passed as the argument in the class name when we will define the object.

Below is the example where we are initializing properties:

class Student {
    var firstName: String
    
    // Initializing property
    init(firstName: String) {
        self.firstName = firstName
    }
}


var student1 = Student(firstName: "Maria")

print(student1.firstName)

Output:

Maria