There are three types of Swift: ** Structures **, ** Classes **, and ** Enums **.
While each has common specifications such as properties and methods, There are also many specifications unique to each of the three.
The difference between these specifications is It extends not only to the presence or absence of simple functions but also to the behavior when passing values.
As mentioned earlier, types include structs, classes, and enums.
These types have a common component and Most data can be represented by structures and classes.
However, I think you'll guess because it's about parting. ** Structures, classes, and enums have their own purpose-specific features. ** **
So, if you select the appropriate type according to the nature of the data, It is possible to express more than just a combination of value and function.
The three types of Swift depend on how values are passed. It can be roughly divided into ** value type ** and ** reference type **.
The difference between the two is whether ** changes are shared with other variables and constants. ** **
Value types are types that do not share changes, whereas values are The type that shares the change is the reference type.
In Swift, ** structs and enums are implemented as value types, and classes are implemented as reference types. ** **
The value type is a type in which the ** instance represents the value itself, not a reference to the value. ** **
Assigning a value type instance to a variable or constant is It means the assignment of the value itself that the instance represents.
Therefore, multiple variables and constants cannot share an instance of one value type. Therefore, the instance once assigned is immutable unless it is reassigned.
From this, ** The advantage of value types is that their values can be predicted. ** **
There are many value types provided by the standard library, Among them, the value type used in particular is the Int type.
Please see the supplement code below.
The variable a is assigned to the variable b, and the assigned a is It is not a reference to the value that a has, but the value 4 that a has.
** That is, because a and b have separate instances of 4, Even if you reassign to a, the value of b does not change and remains at 4. ** **
var a = 4
var b = 4
a = 2
print(a)
print(b)
Execution result
2
4
mutating
For value types, by adding the muting keyword to the method declaration You can perform the process of changing its own value.
When you execute a method with the mutating keyword and change the value of the instance, ** Implicit reassignment to the variable where the instance is stored. ** **
Since the method with the mutating keyword specified is treated as a reassignment method, Executing against an instance of the value type stored in a constant will result in a compile error.
The description method is as follows.
mutating func method name(argument) ->Return type{
Statement executed when a method is called
}
In the sample code, using the extension, I'm adding an increment () method to an Int type.
Because this method can be executed like a.increment ()
At first glance, it looks like you are changing the value without assignment.
However, since reassignment is actually being performed, Executing the increment method on the constant b will result in a compilation error.
extension Int {
mutating func increment() {
self += 1
}
}
var a = 1
a.increment() // 2
let b = 1
b.increment() //Compile error
Error details: Cannot use mutating member on immutable value:'b' is a'let' constant
Japanese translation: change members cannot be used for invariant values: "b" is a "let" constant
If you don't add the mutating keyword, it will not be treated as a reassignment. If you define a method without mutating at the beginning You will not be able to access your own values in the first place.
In other words, the advantage of using matating is ** ・ You can change your own value. -Cannot be executed for constants. ** ** I think that can be mentioned.
The second advantage is useful if you want to prevent changes in the values held by the instance.
Preventing value changes means This means that you can prevent unexpected value assignments.
In other words, safety is improved.
A reference type is a type in which an instance represents a reference to a value. ** ** Classes in Swift are reference types.
Assigning reference type values to variables and constants Because it means assigning a reference to an instance ** Multiple constants and variables can share one reference type instance. ** **
Value types ensured that variables and constants were unaffected by changes in other values.
On the other hand, in the reference type, one instance is shared with other variables and constants. Changes to a value also affect other variables and constants that share an instance.
Even if you write letters, it will be difficult to convey, so I actually wrote the sample code.
I have defined a class Sample type.
var a = Sample(value: 1)
var b = a
Sample (value: 1) is assigned to the variable a, and a is assigned to the variable b.
Assigning a, which is an instance of a reference type, to b means ** It means that b also refers to the instance referenced by a. ** **
So a and b have the same reference to Sample (value: 1)
.
Therefore, changing a.value will change b.value as well.
class Sample {
var value: Int
init(value: Int) {
self.value = value
}
}
var a = Sample(value: 1)
var b = a
a.value // 1
b.value // 1
a.value = 2
a.value // 2
b.value // 2
Value types do not share changes to variables or constants. Therefore, ** once assigned a value is immutable unless explicitly reassigned. ** **
On the other hand, the reference is the opposite, and changes to variables and constants are shared. Therefore, it is difficult to guarantee that the value once assigned will not be changed. ** **
Considering these properties, ** Proactively use value types to handle data safely I think it's best to limit reference types to the extent that changes need to be shared. ** **
This is the end of the explanation of the basic knowledge of types.
I also have an article explaining structures, classes, and enums. Please take a look when you have time!
・ [Swift] Type type ~ Structure ~ ・ [Swift] Type Type-Class Part 1- ・ [Swift] Type type ~ Enumeration ~
Thank you for watching until the end.
Recommended Posts