In Swift, structs are value types whereas classes are reference types. When you copy a struct, you end up with two unique copies of the data. When you copy a class, you end up with two references to one instance of the data. It’s a crucial difference, and it affects your choice between classes or structs. (+ Class extendable, struct does not.)
2. What’s the difference between var
and let
? Which one would you choose for properties in a struct and why?
Both let
and var
are for creating variables in Swift. let
helps you create immutable variables (constants) while on the other hand var
creates mutable variables.
The mutating keyword lets callers know that the method is going to make the value change.