Model your program

Swift Structures

Learn how structures work in Swift, from defining stored properties and methods to why structs are value types that get copied when passed around.

This tutorial belongs to the Swift series

Structures are one of the essential Swift concepts.

They are everywhere in Swift. Even the built-in types are structures: Int, Double, String, arrays, dictionaries. All structs.

From a structure we create instances, which we call objects.

In most languages, objects come only from classes. Swift has classes too, but you can create objects from structures as well, and the official documentation advises you to prefer structures because they are simpler to reason about.

Think of a struct as a lighter version of a class. A struct can have:

  • properties
  • methods (functions)
  • subscripts
  • initializers
  • protocol conformance
  • extensions

The one big thing classes have and structs don’t is inheritance. If you need that, use a class.

Defining a struct

This is the simplest possible struct:

struct Dog {

}

Inside it you define stored properties, the values every instance carries around:

struct Dog {
    var age = 8
    var name = "Roger"
}

This definition creates a new type, Dog. To create an instance, call the type like a function:

let roger = Dog()

Once you have an instance, you read its properties with the dot syntax:

let roger = Dog()
roger.age  // 8
roger.name // "Roger"

The same dot syntax updates a property, but only on a var instance:

var roger = Dog()
roger.age = 9

Try that on a let instance and the compiler says Cannot assign to property: 'roger' is a 'let' constant. A struct declared with let is frozen, all the way down.

The memberwise initializer

You can also create an instance by passing the property values. Swift writes this initializer for you:

let syd = Dog(age: 7, name: "Syd")
syd.age  // 7
syd.name // "Syd"

The parameters follow the order the properties are declared in. Swap them and the code doesn’t compile. Properties declared with let that already have a value are left out, because there’s nothing to set.

Methods

Structures can have instance methods: functions that belong to an instance and can use its properties.

struct Dog {
    var age = 8
    var name = "Roger"
    func bark() {
        print("\(name): wof!")
    }
}

Call it with roger.bark(), and you get Roger: wof!.

There are also type methods, marked with static. They belong to the type itself, not to any instance:

struct Dog {
    var age = 8
    var name = "Roger"
    func bark() {
        print("\(name): wof!")
    }
    static func hello() {
        print("Hello I am the Dog struct")
    }
}

You call it as Dog.hello(), with no instance involved.

Structs are value types

Structures are value types. When you pass a struct to a function, return it from a function, or assign it to another variable, Swift copies it:

var roger = Dog()
var copy = roger
copy.age = 12

roger.age // still 8

copy and roger are two independent values. Change one and the other doesn’t move. This is a big reason structs are easier to reason about than classes, where two variables can point to the same object. We’ll see that difference in the classes lesson.

Lesson completed