This time, I will summarize the differences between classes and structs as a memorandum.
Classes and structs can be divided into value type
and reference type
depending on the value passing method
.
The "type" here is not a String type or an Int type.
It represents a mechanism in which how to handle an object is predetermined
.
And the biggest difference between the two is whether to share changes with other variables or constants
.
· Value types do not share changes`` (eg struct, enum) · Reference types
share changes`` (eg class)
In fact, value types are not just structs, but also enums
.
A type in which the instance represents the value itself, not a reference to it.
In other words, once assigned, the instance It has the advantage of being invariant unless reassigned and its value predictable.
Let's see how it works in code.
Let's see what happens to the passing of instances of a structure called Color type that expresses color in RGB type.
First, define a struct Color and prepare three variables. Then, assign the Color defined earlier to the variable a. Then assign a to the variable b to change the red value of a.
struct Color {
var red: Int
var blue: Int
var green: Int
}
var a = Color(red: 255, blue: 0, green: 0) //Substitute red for variable a
var b = a //Substitute a for variable b
a.red = 0 //Change red of a to black
//Result ↓
//a turned black...
a.red // 0
a.blue // 0
b.green // 0
//b remains red!
b.red // 255
b.blue // 0
b.green // 0
Looking at the results, changing a has no effect on b
.
What you can see here is that no matter how much you change a, b is not affected.
From the above analogy, we can see that the value type is copying the value each time it is assigned to a variable / constant or passed to a function
.
A type in which the instance represents a reference to a value.
Unlike the value type, there is no copy of the instance when assigning to a variable / constant or when passing to a function, so there is an advantage that efficient passing to an instance is possible.
As an example, let's look at a reference type class that has an Int type value as a property.
Assign the IntBox class defined for the variable a, and assign a to the variable b.
Assigning a to the variable b means that the instance IntBox (value: 1) referenced by a is also referenced by b
.
It means that.
In other words, variables a and b have the same instance IntBox (value: 1)
.
So, if you change the value of variable a as shown below, the value of variable b will also change.
class IntBox {
var value: Int
//Initialize
init(value: Int) {
self.value = value
}
}
var a = IntBox(value: 1) //a is IntBox (value):Refer to 1)
var b = a //b refers to the same instance as a
//Both are 1
a.value // 1
b.value // 1
// a.Substituting 2 for value...
a.value = 2
// b.The value of value also changes to 2!
a.value // 2
b.value // 2
So how do you use these two properly when actually developing? I don't know the details either, so I looked it up.
There were some articles that were easy to understand when I looked them up, so I will post them ↓ How to use structure and class properly in Swift (point) [Swift] Use of structure and class properly Note on how to use Swift classes and structures properly
If you find something wrong, please feel free to comment.
Recommended Posts