Swift筆記

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容