> For the complete documentation index, see [llms.txt](https://pakornpat.gitbook.io/ios-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pakornpat.gitbook.io/ios-app/swift/enum.md).

# Enum

เราใช้ Enum ในการสร้ง Type ใหม่ของ ของที่เกี่ยวข้องกัน ที่มักเป็น Set จำกัดเพื่อที่เราจะไม่ต้อง hardcode เป็น String หรืออื่น ๆ เพื่อความปลอดภัยในการเขียนโค้ด เช่น เรามี enum ที่ใช้ในการระบุประเภทการชำระเงินที่ระบบรองรับ

```swift
enum PaymentType {
    case cheque
    case creditCard
}
```

โดยทั่วไป ภาษาอื่นจะมี Enum ที่ใช้ในการแยกประเภทเท่านั้น และมี `Int` ในการระบุ order หรือใช้ระบุ case enum เอง แต่ใน Swift เราสามารถที่จะกำหนดให้มี raw value ของ Enum สามารถเก็บเป็น `Int` `String` หรืออื่น ๆ ได้ เช่น

```swift
enum PaymentType: String {
    case cheque = "CHEQUE"
    case creditCard = "CARD"
}
```

เราสามารถ Get ค่าออกมาได้ผ่าน `rawValue`&#x20;

```swift
PaymentType.creditCard.rawValue
```

ทั้งยังสามารถ create ได้จาก `rawValue` ซึ่งมักจะใช้ในตอนที่เราต้องแปลงค่าที่ได้รับมา เช่น จากการเรียก API หลังบ้าน

```swift
let payment = PaymentType(rawValue: "CARD")
```

นอกจาก `rawValue` แล้ว Enum ใน Swift ยังมี Associated Values ด้วยนั่นคือ เราสามารถเก็บค่าพ่วงเข้าไปในแต่ละเคสแยก ๆ กันไม่เหมือนกันในแต่ละเคสได้ด้วย เช่น เคส creditCard ต้องการเก็บค่า creditCardNumber ซึ่งในเคสของ cheque ไม่ได้ต้องการเก็บ แบบนี้

```swift
enum PaymentType {
    case cheque
    case creditCard(number:String)
}

let payment = PaymentType.creditCard(number: "12345")
```

Enum ยังสามารถมีสิ่งที่เรียกว่า Compute Properties นั่นคือ เราสามารถเพิ่ม property ให้ enum ได้ด้วย แต่เพียงว่ามันไม่สามารถเก็บค่าเอาไว้ได้

เช่น เราต้องการ property description ที่จะใช้อธิบายเคสของ Enum ในขณะนั้น&#x20;

```swift
enum PaymentType {
    case cheque(issuer:String)
    case creditCard(number:String)

    var description: String {
        switch self {
        case let .cheque(issuer): return "Cheque issued by \(issuer)"
        case let .creditCard(number): return "Credit card number \(number)"
        }
    }
}
```

ทำให้เราสั่ง `.description` ได้ แบบนี้

```swift
PaymentType.cheque(issuer: "ABC").description
// Cheque issued by ABC
```

นอกจากนี้ Enum ยังสามารถมีฟังก์ชันได้ด้วย เช่น

```swift
enum PaymentType {
    case cheque(issuer:String)
    case creditCard(number:String)

    var description: String {
        switch self {
        case let .cheque(issuer): return "Cheque issued by \(issuer)"
        case let .creditCard(number): return "Credit card number \(number)"
        }
    }

    func description(with name: String) -> String {
        return String(format: "Name: %@, Type: %@", name, self.description)
    }
}

PaymentType.cheque(issuer: "ABC").description(with: "Pakornpat")
```

ในกรณีที่ฟังก์ชันนั้นเปลี่ยนแปลงค่าตัว Enum เอง จะต้องใส่ `mutating` ที่ฟังก์ชันด้วย (ใส่เพื่อให้อ่านแล้วรู้ว่ามีการเปลี่ยนค่าตัวเอง ซึ่งจะสัมพันธ์กับที่เราใช้ `let` กับ `var`)

```swift
enum TapSwitch {
    case on
    case off

    var description: String {
        switch self {
        case .on: return "current is on"
        case .off: return "current is off"
        }
    }

    mutating func toggle() {
        switch self {
        case .on:
            self = .off
        case .off:
            self = .on
        }
    }
}

var state = TapSwitch.on
state.toggle()
state.description
```
