Swift Structures

This tutorial belongs to the Swift series

Structures are one essential Swift concepts.

Structures are everywhere in Swift. Even the built-in types are structures.

We can create instances of structures, which we call objects.

In most lang…

This tutorial belongs to the Swift series

Structures are one essential Swift concepts.

Structures are everywhere in Swift. Even the built-in types are structures.

We can create instances of structures, which we call objects.

In most languages, objects can only be created from classes. Swift has classes, too, but you can create objects also from structures and the official documentation advises to prefer structures because they are easier to use.

They are a light versions of classes.

A struct can have properties.
A struct can have methods (functions)
A struct can define subscripts
A struct can define initializers
A struct can conform to protocols
A struct can be extended

One important thing classes allow is inheritance, so if you need that, you have classes.

A struct is defined using this syntax:

struct Dog {

}

Inside a structure you can define stored properties:

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

This structure definition defines a type. To create a new instance with this type, we use this syntax:

let roger = Dog()

Once you have an instance, you can access its properties using the dot syntax:

let roger = Dog()
roger.age
roger.name

The same dot syntax is used to update a property value:

roger.age = 9

You can also create a struct instance passing the values of the properties:

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

To do so, properties must be defined variables, with var, not as constants (with let). It’s also important to respect the order those properties are defined.

Structures can have instance methods: functions that belong to an instance of a structure.

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

And we also have type methods:

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

Invoked as Dog.hello()

Structures are a value type. This means they are copied when passed to a function, or when returned from a function. And when we assign a variable pointing to a structure to another variable.

This also means that if we want to update the properties of a structure we must define it using var and not let.

All types in Swift are defined as structures: Int, Double, String, arrays and dictionaries, and more, are structures.


Print Share Comment Cite Upload Translate
APA
flaviocopes.com | Sciencx (2024-03-29T07:11:11+00:00) » Swift Structures. Retrieved from https://www.scien.cx/2021/06/09/swift-structures/.
MLA
" » Swift Structures." flaviocopes.com | Sciencx - Wednesday June 9, 2021, https://www.scien.cx/2021/06/09/swift-structures/
HARVARD
flaviocopes.com | Sciencx Wednesday June 9, 2021 » Swift Structures., viewed 2024-03-29T07:11:11+00:00,<https://www.scien.cx/2021/06/09/swift-structures/>
VANCOUVER
flaviocopes.com | Sciencx - » Swift Structures. [Internet]. [Accessed 2024-03-29T07:11:11+00:00]. Available from: https://www.scien.cx/2021/06/09/swift-structures/
CHICAGO
" » Swift Structures." flaviocopes.com | Sciencx - Accessed 2024-03-29T07:11:11+00:00. https://www.scien.cx/2021/06/09/swift-structures/
IEEE
" » Swift Structures." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/09/swift-structures/. [Accessed: 2024-03-29T07:11:11+00:00]
rf:citation
» Swift Structures | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/09/swift-structures/ | 2024-03-29T07:11:11+00:00
https://github.com/addpipe/simple-recorderjs-demo