Enumeration
Enumeration เป็นการกำหนด Type สำหรับชุดข้อมูลหนึ่งๆ ซึ่งมีความเกี่ยวข้องกันไว้ด้วยกัน เพื่อเป็นรายการที่ง่ายต่อการเรียกใช้ และลดข้อผิดพลาดจากการกำหนดค่านอกขอบเขต
การกำหนดค่า Enummeration เพื่อเป็นการสร้างชนิดข้อมูลแบบตัวเลือก สำหรับนำไปกำนดค่าที่เป็นไปได้ในเรื่องที่สนใจ เช่น ประเภททิศทางของเข็มทิศ เราจะทำได้ดังนี้
enum CompassPoint {
case north
case east
case south
case west
}
var compassHeading:CompassPoint = .north
switch compassHeading {
case .north:
print("I am heading north")
case .east:
print("I am heading east.")
case .south:
print("I am heading south")
case .west:
print("I am heading west")
}
แหล่งข้อมูลอ้างอิง
The Swift Programming Language (Swift 5.5), Apple Inc., 2019. Available on: Apple Book Store.
App Development with Swift, Apple Inc., 2017. Available on: Apple Book Store.
Last updated
Was this helpful?