## STRUCTS
Structs(結構體) 是一種復雜數據類型,包含了多個值,通過 struct 關鍵字定義一個結構體:
~~~
struct Person {
var clothes: String
var shoes: String
}
~~~
Swift 讓你非常簡單地創建一個結構體變量,只需要將初始值傳入即可:
~~~
let taylor = Person(clothes: "T-shirt", shoes: "sneakers")
let other = Person(clothes: "short skirts, shoes: "high heels")
~~~
通過結構體變量名以及屬性名來訪問屬性的值:
~~~
print(taylor.clothes)
print(other.shoes)
~~~
Swift 有一個名為"copy on write"的機制,當你將一個結構體變量賦給另一個變量時,會獨立拷貝一份:
~~~
struct Person {
var clothes: String
var shoes: String
}
let taylor = Person(clothes: "T-shirts", shoes: "sneakers")
let other = Person(clothes: "short skirts", shoes: "high heels")
var taylorCopy = taylor
taylorCopy.shoes = "flip flops"
taylor //(clothes: "short skirts", shoes: "high heels")
taylorCopy //(clothes: "short skirts", shoes: "flip flops")
~~~