Skip to content

Instantly share code, notes, and snippets.

@totiz
Last active May 23, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totiz/2c9e035156960a11bb937f08c4e7b0de to your computer and use it in GitHub Desktop.
Save totiz/2c9e035156960a11bb937f08c4e7b0de to your computer and use it in GitHub Desktop.
Thing
struct Thing {
var name: String
// ตัวแปรระดับ Global สามารถเข้าถึงได้จากทุกที่
static var globalThings = [String]()
// บัณทึกชื่อไว้ใน globalThings แม้ว่าตัวแปร Thing จะถูกลบ ค่าที่เก็บไว้จะคงอยู่ต่อไป
func saveToGlobal() {
Thing.globalThings.append(name)
}
}
// สร้างตัวแปร Thing และบัณทึกเก็บไว้ในระดับ Global
let Dog = Thing(name: "Puppy")
Dog.saveToGlobal()
Thing(name: "Swift").saveToGlobal()
// แสดงค่าที่เก็บไว้ในระดับ Global ทั้งหมด
print(Thing.globalThings) // "["Puppy", "Swift"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment