Swift Structs

In this article, we will explain structs in Swift.

Basics of Swift Structs

In Swift, a struct can hold properties that store values and methods that provide related functionality. You can define a struct once and then create instances of it as needed.

The syntax for structs and classes is similar, but structs cannot be subclassed and do not support deinitializers, among other differences.

You can define an initializer init() in a struct, but even if you don't, Swift provides a default initializer so you can still create an instance by passing property values as arguments.

Since classes are more complex, the Swift documentation recommends using structs unless you specifically need the extra features of classes.


To define a struct in Swift, use the struct keyword as follows:

struct StructName {
    var property1: Type1
    var property2: Type2

    func methodName() {
        // method body
    }
}

For example, here's a struct called Person with name and age properties and a greet method:

struct Person {
    var name: String
    var age: Int
    
    func greet() {
        print("Hello! My name is \(name), and I'm \(age) years old.")
    }
}

How to Use Structs in Swift

To create an instance of a struct, you can do the following:

var variableName = StructName(property1: value1, property2: value2, ...)

As with other types, you can also use let instead of var if the instance does not need to change.

Properties can have default values. If they don't, you must provide values when creating the instance. If defaults are provided, those arguments become optional at initialization.


To update a property, use StructName.property = newValue. To call a method, use StructName.method().


For example, let's use the Person struct defined earlier:

struct Person {
    var name: String
    var age: Int
    
    func greet() {
        print("Hello! My name is \(name), and I'm \(age) years old.")
    }
}

var p1 = Person(name: "Emma", age: 15)
p1.greet()

p1.age = 20
p1.greet()

The result will be:

Hello! My name is Emma, and I'm 15 years old.
Hello! My name is Emma, and I'm 20 years old.

Structs are Value Types in Swift

Another important difference between structs and classes in Swift is that structs are value types, while classes are reference types.

For example, if you copy a struct into a new variable and then update a property, the original struct's properties remain unchanged because they are separate instances:

struct Person {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
    
    func greet() {
        print("Hello! My name is \(name), and I'm \(age) years old.")
    }
}

var p1 = Person(name: "Emma", age: 15)

var p2 = p1
p2.age = 20

p1.greet()
p2.greet()

The result shows that changing p2 does not affect p1:

Hello! My name is Emma, and I'm 15 years old.
Hello! My name is Emma, and I'm 20 years old.

Swift Struct Example 1

If you change struct to class in this code, then p1 and p2 will reference the same instance, so modifying one also affects the other.


That wraps up our explanation of structs in Swift.