Structures and Classes
2021-08-03 本文已影响0人
宋奕Ekis
Comparing Structures and Classes
Memberwise Initializers for Structure Types
struct Resolution {
var width = 0
var height = 0
}
let vga = Resolution(width: 640, height: 480)
The structure alway has a memberwise initializer, but class dose not have.
Structures and Enumerations Are Value Types
In fact, all of the basic types in swift are value types.
But be careful about the collection types, such as arrays and dictionaries and strings, which use an optimization to reduce the performance cost of copying. It is copied only before the copies are modified, otherwise those copies will share the memory with the original instance.
Classes Are Reference Types
Identity Operators
- Identical to (
===
) - Not identical to (
!==
)
Use these operators to check whether two constants or variables refer to the same single instance.
Let’s think!