2. When and How to Use Value and Reference Types in Swift
Value types and Reference types
Structs in Swift can define attributes and methods, specify initialization methods, and implement protocols. Structs can do almost anything a class can do except inheritance, so it becomes a question when to use structs and when to use classes. In other words, in Swift, when to use value types. When are reference types used?
- Value types: Struct, Enum, Tuple
- Reference types: Class, Function
The semantic
- Value types: Variables are logically aligned with the data (value) assigned to them. They are generally considered to be stored on the Stack, but in reality, some of the data is stored in the CPU registers and some of the data is still allocated on the Heap. This can be interpreted sensibly as meaning that data of a value type (value) is contained in a variable and cannot be operated on alone.
- Reference types: is the opposite of value type, variable and its distribution of data (instance) is separate, instance of a reference type distribution on pile (Heap), variable is allocated on the stack, only hold the instance in memory address, you can have multiple variables point to the same instance, the instance can be independently of any point to its operating variables.
/ / value types
struct CatStruct {
var name: String
}
let a = CatStruct(name: "Whiskers")
var b = a
b.name = "Fluffy"
print(a.name) // Whiskers
print(b.name) // Fluffy
// Reference type
class CatClass {
init(name: String) {
self.name = name
}
var name: String
}
let x = CatClass(name: "Whiskers")
let y = x
y.name = "Fluffy"
print(x.name) // Fluffy
print(y.name) // FluffyCopy the code
Differences in the concept of Mutability
- Distinguish variable variability from instance variability
- Because value types, variables and instances are logically unified, variable variability of value types is the variability of instances
- Reference type and variable are separated from the instance they point to. Therefore, the variability of a variable only means that it always points to the same instance, not the variability of the actual instance. The specific data (attributes) in the actual instance still maintain their own variability.
Properties of value types
- Attribute-based Equality
- Lack of Identity or lifecycle
- Substitutability (Substitutability)
- These three features sum up to this: $5 is $5, $5 everywhere
Advantages of value types
- Efficient: Reference types are allocated on the heap, which is more expensive than allocation on the stack. In Swift, value types implement copy on write based on their nature, which allocates memory space for mutable value types only when they actually change.
- Side-effect free (predictable) : Unlike instances of reference types that can be referenced and manipulated by multiple variables, the nature of value types can effectively avoid side effects in code.
- Thread safety
- There are no memory leaks
- Easy to test
How are value types and reference types designed and used in application development
- Reference types are used to build model entities with identities.
-
- If the value type is used in this case, it is easy to cause inconsistent data (attributes) of the same identity
- Value types encapsulate state, expose behavior, and express business logic
- The use environment is very important
- Equitability tests based on attributes
- A combination of value types and reference types
-
- Constructs an object on a value basis
conclusion
Swift provides powerful and efficient value types that make some of our code more efficient, side-effect-free, and thread-safe. On the premise of understanding the difference between value type and reference type, combining these two data types can achieve the application goal better and more efficiently.