μ΄κ±°ν (Enumerations)
enum CompassPoint {
case north
case south
case east
case west
}
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
- μ½€λ§λ‘ ꡬλΆνμ¬ νμ€λ‘ νκΈ°ν μ μλ€.
enum Beverage: CaseIterable {
case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
- μ΄κ±°ν μ΄λ¦ λ€μ : CaseIterable μ μμ± → allCases νλ‘νΌν°λ arrayλ‘ κ΄λ ¨ λ©μλ μ¬μ© κ°λ₯
μ°κ΄λ κ° (Associated Values)
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
- μΌμ΄μ€ κ°κ³Ό ν¨κ» λ€λ₯Έ νμ μ κ°μ μ μ₯ν μ μλ κ²
μμκ° (Raw Values)
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
- λμΌν νμ μ κΈ°λ³Έκ° μ€μ κ°λ₯
- λͺ¨λ μΌμ΄μ€κ° μμκ°μ κ°μ§ νμλ μλ€.
- λ€μ μΌμ΄μ€μ μμκ°μ νλμ© μ¦κ°, 첫λ²μ§Έ μΌμ΄μ€μ κ° μ€μ μ΄ μλμ΄ μμΌλ©΄ 0 μΌλ‘ μ€μ νλ€.
μμκ°μΌλ‘ μ΄κΈ°ν (Initializing from a Raw Value)
let possiblePlanet = Planet(rawValue: 7) // Planet.uranus
- μ΅μ λ νμ μ → rawValueμ ν΄λΉνλ μΌμ΄μ€κ° μλ€λ©΄ nil 리ν΄
μ¬κ· μ΄κ±°ν (Recursive Enumerations)
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
- νλ μ΄μμ μ°κ΄λ κ°μΌλ‘ μ΄κ±°νμ λ€λ₯Έ μΈμ€ν΄μ€λ₯Ό κ°μ§κ³ μλ μ΄κ±°ν
- μ¬κ·μ μμ λνλ΄κΈ° μν΄ μΌμ΄μ€ μμ± μ μ indirect ν€μλλ‘ νμ
'Swift Language Guide' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Swift Language Guide] νλ‘ν μ½ (Protocols) (0) | 2023.08.01 |
---|---|
[Swift Language Guide] ꡬ쑰체μ ν΄λμ€ (Structures and Classes) (0) | 2023.06.05 |
[Swift Language Guide] ν΄λ‘μ (Closures) (0) | 2023.06.05 |
[Swift Language Guide] ν¨μ(Functions) (0) | 2023.05.24 |
[Swift Language Guide] μ μ΄νλ¦(Control Flow) (0) | 2023.05.24 |