5 concepts · 10 practice questions
Structs and enums are value types: assigning or passing one makes an independent copy, so a change to the copy never affects the original. Classes are reference types: every variable points at one shared instance, and identity can be compared with ===. A let constant of a struct type is fully immutable, while a let class reference can still have its var properties changed. Swift favors value types for models because they carry no hidden shared state.
struct Point { var x = 0 }
final class Box { var x = 0 }
var a = Point()
var b = a
b.x = 5 // a.x is still 0: b is a copy
let c = Box()
let d = c
d.x = 5 // c.x is now 5: one shared instance
print(c === d) // true, same identity
Sign in to read the rest and take the 10-question test.
Sign in to continue