Swift Structures (Structs)


In this Swift programming tutorial, you are going to learn about Structures or just structs with the help of examples.

A structure or struct in Swift is a complex data type. That means structures can be built with multiple values of different data types.

Structures can have their own variables and constants of different data types, can have their own functions that can be used however you want in the program during app development.

The purpose of using a struct in Swift is to construct the building blocks of the code of your program. We can extend the functionality of a Swift structure by defining different types of properties and methods.

Now, let’s see a real-world example:

Suppose you have to store the price and color of a car. If there is only one car then you can store it easily by creating two variables, for example, carPrice and carColor.

Now suppose, you have to store the price and color of multiple cars than what will you do? Will you create multiple variables?

But this doesn’t make sense. It might be a tedious task for creating a lot of variables. In this situation, a struct can be used to overcome it. A single structure can be used for multiple cars.

Basically, you can think of a struct as a blueprint. You can have a blueprint of the properties of a car, for example, the color of a car, engine type, wheel, steering and so on. Using your blueprint a real car can be built

Another example is you have drawn a sketch of a house. In this case, the sketch is like the structure. Using it, one can build a real house. There is only one sketch, but you can build as many houses as you want from the sketch.

Define a Structure

In Swift, we use the struct keyword to define a structure followed by the name of the structure. Below is the syntax:

struct nameOfStructure {
   // properties and methods
}

In the above syntax, the nameOfStructure refers to the name of the struct. Inside the curly brackets, we have all the properties and methods.

Now let’s create a structure in Swift having price and color of car:

struct Car {
    // Properties
    var carPrice: Float
    var carColor: String
}

Instances of a structure

We have just defined a structure. But a definition of a struct is just a blueprint. To use a structure we have to create the instance of our class.

Below is how we can do it:

struct Car {
    // Properties
    var carPrice: Float
    var carColor: String
}

// create instance of struct
var car1 = Car(carPrice: 21000, carColor: "Red")

In the above code we have created an instance of our struct by the name of the struct and then pass the properties arguments. Variables inside the structure are known as properties.

Well, the argument during creating instances can be optional, if we set default values in properties during creating the structure. You can see in the example below:

struct Car {
    // Properties with default values
    var carPrice = 21000
    var carColor = "Red"
}

// create instance of struct
var car1 = Car()

print(car1.carPrice)
print(car1.carColor)

After we run the above program, we will get the output given below:

21000
Red

Accessing properties of a structure

We can access the properties of a struct. Below is how we can do it:

struct Car {
    // Properties
    var carPrice: Float
    var carColor: String
}

// create instance of struct
var car1 = Car(carPrice: 21000, carColor: "Red")

//Accessing properties
print(car1.carPrice)
print(car1.carColor)

Output:

21000.0
Red

In the above code, we are able to access the properties using the variable name of the struct instance followed by dot (.) notation and then the property name.

Modify and assigning new value to properties

We can modify the value of struct properties and also can assign new values to the properties of a Swift structure easily.

See the code below where we are modifying the values of properties:

struct Car {
    // Properties
    var carPrice: Float
    var carColor: String
}

// create instance of struct
var car1 = Car(carPrice: 21000, carColor: "Red")

// Print properties before assigning new values
print(car1.carPrice)
print(car1.carColor)

//Assigning new values to struct properties
car1.carPrice = 32000.5
car1.carColor = "Black"

// Print properties after assigning new values
print(car1.carPrice)
print(car1.carColor)

Output:

21000.0
Red
32000.5
Black

In the above code, we have printed struct properties before and after changing their values.

Defining multiple instances of the same structure

We can create multiple instances of the same struct. It will increase the reusability of our code. We can create as many instances as we want.

struct Car {
    // Properties
    var carPrice: Float
    var carColor: String
}

// create instance of struct
var car1 = Car(carPrice: 21000, carColor: "Red")

print(car1.carPrice)
print(car1.carColor)

// create another instance of struct
var car2 = Car(carPrice: 32000.5, carColor: "Black")

print(car2.carPrice)
print(car2.carColor)

Output:

21000.0
Red
32000.5
Black

In the above code, we have created two instances with names car1 and car2.

Creating method inside Swift structures

We can create a method inside a struct.

A method is just a function, but it is defined inside a structure. That means, if we create a function inside a structure, it will be then called a method.

Below is how we are creating a method inside a struct:

struct StructWithMethod {
    func helloMsg() {
        print("Hello World")
    }
}

// Creating instance
var message1 = StructWithMethod()

// Accessing struct method
message1.helloMsg()

In the above code, we have created a structure with the name structWithMethod and then created a function inside this struct. The function is called a method.

Next, we create the instance of the struct and finally access the method.