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(枚举)是数值类型
Classes Are Reference Types
而Class(类)是引用类型
和class类的定义不一样。class的定义赋值,只是赋值指针,所以不会直接是对变量的更改
摘录来自: Apple Inc. “The Swift Programming Language (Swift 2.1)”。 iBooks. https://itun.es/cn/jEUH0.l
网友评论