Last active
May 23, 2016 10:10
-
-
Save totiz/2c9e035156960a11bb937f08c4e7b0de to your computer and use it in GitHub Desktop.
Thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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