Swift Structs

In Swift, a struct is used to store variables of different data types. For example,

Suppose we want to store the name and age of a person. We can create two variables: name and age and store value.

However, suppose we want to store the same information of multiple people.

In this case, creating variables for an individual person might be a tedious task. To overcome this we can create a struct that stores name and age. Now, this struct can be used for every person.


Define Swift Structure

Here's the syntax to define struct in Swift:

struct StructureName {
  // structure definition 
}

Here,

  • struct - keyword used to define a structure
  • StructName - the name of the structure

Let's see an example,

struct Person {

  var name = ""
  var age = 0
}

Here, we have defined a struct named Person. Inside the curly braces {}, the struct contains two variables name and age with values "" and 0 respectively.

Note: The variables and constants inside a struct are called properties.


Struct Instances

A struct definition is just a blueprint. To use a struct, we need to create an instance of it. For example,

struct Person {

  var name = " "
  var age = 0
}

// create instance of struct
var person1 = Person()

Here, we have created an instance by writing the name of the structure Person followed by a default initializer ()

Now, we can use the person1 instance to access and modify the struct properties. For example,

// modify the name property
person1.name = "Swift"

// access the age property
person1.age

Here, we have used the dot notation . to access struct properties.


Example: Swift Access Struct Properties

// define a structure 
struct Person {

// define two properties
 var name = ""
 var age = 0
}

// create instance of Person
var person1 = Person()

// access properties and assign new values
person1.age = 21
person1.name = "Rick"

print("Name: \(person1.name) and Age: \( person1.age) ")

Output

Name: Rick and Age: 21

In the above example, we have defined the struct named Person with two properties: name and age.

We have also created an instance person1 of the struct Person.

Finally, we have accessed and modified the properties of an instance using . notation.


Example: Create Multiple Instances of Struct

// define a structure
struct Student {

// define a property
var studentID = 0
}

// instance of Person
var student1 = Student()

// access properties and assign new values
student1.studentID = 101

print("Student ID: \(student1.studentID)")

// another instance of Person
var student2 = Student()

// access properties and assign new values
student2.studentID = 102

print("Student ID: \(student2.studentID)")

Output

Student ID: 101
Student ID: 102

In the above example, we have created two instances student1 and student2 of the Student struct

Note: We can create as many instances of the structure as we want.


Swift Memberwise Initializer

Earlier we assigned a default value to a struct property.

struct Person {
  
  var name = ""
}

We have created an instance using the default initializer.

var person1 = Person()

However, if we do not assign a default value to struct property.

struct Person {
  
  var name: String
}

We pass value while creating an instance

var person1 = Person(name: "Kyle")

Here, values inside the parenthesis () are automatically assigned to corresponding properties of the struct. This is called a memberwise initializer.

Note: We can also use a memberwise initializer even if we have assigned a default value to a struct property.


Example: Memberwise Initializer

struct Person {

// properties with no default values
var name: String
var age: Int
}

// instance of Person with memberwise initializer  
var person1 = Person(name: "Kyle", age: 19)

print("Name: \(person1.name) and Age: \( person1.age)")

Output

Name: Kyle and Age: 19

In the above example, we have created the instance person1 for the Person struct.

Since we have not assigned default values to the properties, we use a memberwise initializer to assign values.


Function Inside Swift Struct

We can also define a function inside a swift struct. A function defined inside a struct is called a method.

Let's see an example,

struct Car {

  var gear = 0

  // method inside struct
  func applyBrake(){
  print("Applying Hydraulic Brakes")
  }
}

// create an instance 
var car1 = Car()

car1.gear = 5

print("Gear Number: \(car1.gear)")
// access method
car1.applyBrake()

Output

Applying Hydraulic Brakes

In the above example, we have defined the method named applyBraking() inside the Car struct. Notice the accessing of the method,

car1.applyBrake()

Here, we have used . notation to access method defined inside the struct. Finally, the statement inside the method is executed.