/

Swift Structures - The Fundamentals of Swift Structures

Swift Structures - The Fundamentals of Swift Structures

tags: [“Swift”, “Structures”, “Classes”, “Objects”, “Properties”, “Methods”, “Type Methods”]

Structures are a fundamental concept in Swift programming. They are widely used and even the built-in types in Swift are implemented as structures. In Swift, we can create instances of structures, which are known as objects.

In most programming languages, objects can only be created from classes. However, Swift provides the flexibility to create objects from structures as well. In fact, the official documentation recommends using structures whenever possible because they are easier to work with. Structures can be seen as lightweight versions of classes.

A structure in Swift can have properties, methods (functions), subscripts, initializers, and can even conform to protocols. Structures can also be extended, providing additional functionality.

However, one thing that structures lack compared to classes is inheritance. If you need inheritance, you will have to use classes instead.

To define a structure in Swift, we use the following syntax:

1
2
3
struct Dog {
// properties and methods go here
}

Inside a structure, we can define stored properties:

1
2
3
4
struct Dog {
var age = 8
var name = "Roger"
}

These properties can be accessed using the dot syntax after creating an instance of the structure:

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

You can also modify the values of the properties using the dot syntax:

1
roger.age = 9

Additionally, you can create a structure instance by passing the values of the properties:

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

To support this initialization method, the properties must be defined as variables (var) rather than constants (let). It is also important to respect the order in which the properties are defined.

Structures can also have instance methods, which are functions that belong to an instance of a structure:

1
2
3
4
5
6
7
8
struct Dog {
var age = 8
var name = "Roger"

func bark() {
print("\(name): wof!")
}
}

Moreover, Swift structures can have type methods, which are methods that belong to the structure itself:

1
2
3
4
5
6
7
8
9
10
11
12
struct Dog {
var age = 8
var name = "Roger"

func bark() {
print("\(name): wof!")
}

static func hello() {
print("Hello, I am the Dog struct")
}
}

These type methods can be invoked using the structure name, like Dog.hello().

It is worth noting that structures are value types. This means that they are copied when passed to a function or when returned from a function. They are also copied when assigned to another variable. Therefore, if we want to update the properties of a structure, we must define it using var and not let.

Lastly, it is important to know that all types in Swift, including Int, Double, String, arrays, dictionaries, and more, are defined as structures.

In conclusion, Swift structures are essential in Swift programming and offer a lightweight alternative to classes. They provide properties, methods, and other functionalities, making them versatile and easy to work with. When using structures, keep in mind their value type nature and the missing inheritance feature.