April 2026
Intermediate
683 pages
25h 29m
English
In Chapter 8, you learned a lot about functions of structs. Nearly all this knowledge is applicable to classes too. There are only a few notable differences, which are covered in the following sections.
For structs, methods that modify properties must be marked with the mutating keyword as Swift assumes immutability otherwise. Classes are reference types, however, and their instances are mutable when modified through a reference. In that sense, class functions don’t require the mutating keyword to change property values. This difference is highlighted via an otherwise identical struct and class in Listing 9.7.
struct PersonStruct { var age: Int = 0 mutating func makeOlder() { self.age += 1 }}class PersonClass ...
Read now
Unlock full access