🖥️
iOS App with Pop
UIKit Swift 5
UIKit Swift 5
  • iOS App development
  • Swift
    • Variable & Constant
    • Number & String
    • Operator
    • Array, Dictionary & Tuple
    • Enum
    • Optional
    • Function
    • Class & Struct
    • Branching
    • Loops
    • Error handler
    • Protocol
    • Extension
  • Create New Project
  • Introduction to Xcode
  • Scene-Based Life-Cycle
  • UIViewController
  • Storyboard
  • First Run
  • Display todo list
  • Basic Auto Layout
  • MVC
  • Model
  • Binding TableView
  • Binding TableViewCell
  • TableViewDelegate
  • Add navigationBar with + button
  • Add new item page
  • TextField and Switch
  • Binding action
  • Add mock item to todo list
  • What is weak?
  • Finish add item
  • Delete todo item
  • Edit todo item
  • Custom new layout
  • Adding new delegate
  • Refactor
  • Pushing edit view
  • Large navigation
  • Drag item
  • Drop item (in app)
  • Save data
  • Where to go from here?
Powered by GitBook
On this page
  1. Swift

Enum

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

enum PaymentType {
    case cheque
    case creditCard
}

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

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

เราสามารถ Get ค่าออกมาได้ผ่าน rawValue

PaymentType.creditCard.rawValue

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

let payment = PaymentType(rawValue: "CARD")

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

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

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

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

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

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 ได้ แบบนี้

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

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

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)

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
PreviousArray, Dictionary & TupleNextOptional

Last updated 2 years ago