1.struct的賦值,是將struct的值拷貝過去。如
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
cinema.width = 2048
Checking the width property of cinema shows that it has indeed changed to be 2048:
print("cinema is now \(cinema.width) pixels wide")
// prints "cinema is now 2048 pixels wide"
When cinema was given the current value of hd, the values stored in hd were copied into the new cinema instance. The end result is two completely separate instances, which just happened to contain the same numeric values. Because they are separate instances, setting the width of cinema to 2048 doesn’t affect the width stored in hd.
enum枚舉也是同理
enum CompassPoint {
case North, South, East, West
}
var currentDirection = CompassPoint.West
let rememberedDirection = currentDirection
currentDirection = .East
if rememberedDirection == .West {
print("The remembered direction is still .West")
}
// prints "The remembered direction is still .West
Structures and Enumerations Are Value Types
struct(結構體)和enmu(枚舉)是數(shù)值類型
Classes Are Reference Types
而Class(類)是引用類型
和class類的定義不一樣。class的定義賦值,只是賦值指針,所以不會直接是對變量的更改
摘錄來自: Apple Inc. “The Swift Programming Language (Swift 2.1)”。 iBooks. https://itun.es/cn/jEUH0.l