: case μμμ μ μν μ μλ μ°κ΄λ μμλ€(Enumeration case)μ νλλ‘ λͺ¨μ νμ (μλ£ν)
μ¬μ©μκ° μμλ‘ λ§λ€κ³ μΆμ νμ μ μ μ
λ³μλͺ μ λλ¬Έμλ‘ μμ± νκ³ λ¨μ μ΄λ¦μΌλ‘ μ§μ , λ΄λΆμ caseλ μλ¬Έμλ‘ μμ±,
-> 미리 μ ν΄λ μΌμ΄μ€μμλ§ μ¬μ©μ΄ λκΈ° λλ¬Έμ μ½λμ κ°λ μ±κ³Ό μμ μ±μ΄ μ’μ
//eunmμ ꡬ쑰
enum TypeName {
case caseName
case caseName, caseName //μ½€λ§λ‘ μ¬λ¬ case λμ΄ κ°λ₯
}
// enum μ μ λ°©λ²1
enum Weekend {
case saturday
case sunday
}
// enum μ μ λ°©λ²2
enum Weekend {
case saturday, sunday
}
// μ¬μ©
var today: Weekend = Weekend.sunday
// νμ
νμ
μμ μλ case
today = .saturday // todayμ νμ
μ΄ Weekendλ‘ λͺ
ννκΈ°(13λ²μ§Έ μ½λμμ μ¬μ©) λλ¬Έμ Weekend μλ΅ κ°λ₯
var anotherToday = today // κ° λ³΅μ¬
today = .sunday // κ°μ λ³κ²½ν΄λ anotherTodayμλ μν₯ β
μ΄κ±°νμ forλ¬Έ 보λ€λ switch ꡬ문과 μ£Όλ‘ μ¬μ©λλ€
enum CompassPoint {
case north
case south
case east
case west
}
// ν μΌμ΄μ€ μ μΈ λ°©λ²
var directionToHead = CompassPoint.west
directionToHead = .east //νμ
μ μ΄λ―Έ μκ³ μμΌλ―λ‘ κ°μ μ€μ ν λ νμ
μμ κ°λ₯
// νμ© μμ
directionToHead = .south
switch directionToHead {
case .north:
print("λΆμͺ½")
case .south:
print("λ¨μͺ½")
case .east:
print("λμͺ½")
case .west:
print("μμͺ½") //μ΄κ±°νμ μ μΈλ λͺ¨λ μΌμ΄μ€λ₯Ό 맀μΉμν€κ³ μμ΄ default μλ΅ κ°λ₯
}
// μΆλ ₯κ°: "λ¨μͺ½"
CaseIterableμ Swiftμ νλ‘ν μ½λ‘, μ΄κ±°ν(enums)μ΄ λͺ¨λ μΌμ΄μ€λ₯Ό μλμΌλ‘ 컬λ μ μΌλ‘ μ 곡ν μ μκ² ν΄μ€λ€.
μ΄ νλ‘ν μ½μ μ±νν μ΄κ±°νμ allCasesλΌλ νμ νλ‘νΌν°λ₯Ό κ°μ§κ² λλ©°,
μ΄λ₯Ό ν΅ν΄ ν΄λΉ μ΄κ±°νμ λͺ¨λ μΌμ΄μ€λ₯Ό λ°°μ΄λ‘ μ κ·Όν μ μλ€.
enum Beverage: CaseIterable {
case coffee, tea, juice
}
//allCases: μ΄κ±°νμ λͺ¨λ μΌμ΄μ€λ₯Ό ν¬ν¨νλ μ½λ μ
μ μ κ·Ό
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) μ μ£Όλ¬Έ κ°λ₯ν©λλ€.") // μΆλ ₯κ°: 3μ μ£Όλ¬Έ κ°λ₯ν©λλ€
for beverage in Beverage.allCases {
print(beverage) //μΆλ ₯: coffee, tea, juice
}
μμκ°μ κ° μΌμ΄μ€μ μ¬μ μ μ μλ κ°μΌλ‘, λ³΄ν΅ μ μ, λ¬Έμμ΄, μ€μ λ±μ κΈ°λ³Έ λ°μ΄ν° νμ μ μ¬μ©νλ€.
3-1. μμμ μΌλ‘ ν λΉλ μμκ°
//Raw Valueμ ꡬ쑰
enum TypeName: RawValueType { //RawValueTypeμ String, Character, Number νμ
λ§ κ°λ₯
case caseName = Value //νλ² μ μ₯ν κ°μ λ³κ²½ λΆκ°
}
//μμκ°μ μ€μ νμ§ μμΌλ©΄ Swiftλ μλμ μΌλ‘ κ°μ ν λΉ
//μ μλ₯Ό μμκ°μΌλ‘ μ¬μ©νλ©΄ κ° μΌμ΄μ€μ μμμ κ°μ μ΄μ μΌμ΄μ€λ³΄λ€ νλμ© μ¦κ°.
//첫λ²μ§Έ μΌμ΄μ€μ κ° μ€μ μ΄ μλμ΄ μμΌλ©΄ 0 μΌλ‘ μ€μ
enum Direction: Int {
case north = 1
case south
case east
case west
}
let direction = Direction.east
print("Direction: \(direction.rawValue)") // μΆλ ₯: Direction: 3
//=================================
enum Alignment: Int {
case left
case right = 100
case center
}
//rawValue νλ‘νΌν°λ₯Ό μ¬μ©νμ¬ μ΄κ±°ν μΌμ΄μ€μ μμκ°μ μ κ·Ό
Alignment.left.rawValue //0
Alignment.right.rawValue //100
Alignment.center.rawValue //101
Alignment.center.rawValue = 10 //μμκ°μ μμμ΄κΈ° λλ¬Έμ μλ‘μ΄ κ° ν λΉ λ³κ²½ λΆκ°
Alignment(rawValue: 0) //left
Alignment(rawValue: 200) //nil
//====================================
//μμκ°μΌλ‘ λ¬Έμμ΄μ΄ μ¬μ©λ λ κ° μΌμ΄μ€μ μμμ κ°μ μΌμ΄μ€μ μ΄λ¦μ λ¬Έμ
enum Weekday: String {
case sunday
case monday = "Mon"
case tuesday
}
Weekday.sunday.rawValue //"sunday"
Weekday.monday.rawValue //"Mon"
//====================================
//Character νμ
μ Raw Valueλ₯Ό μ§μ ν΄μ€μΌ νλ€.
enum CompassPoint: String {
case north = "N"
case south = "S"
case east = "E"
case west = "W"
}
let compassPoint = CompassPoint.west
print("Compass Point: \(compassPoint.rawValue)") // μΆλ ₯: Compass Point: W
3-2. μμκ°μΌλ‘ μ΄κΈ°ν
μ΄κ±°νμ΄ μμκ°μ κ°μ§ λ, init?(rawValue:) μ΄λμ λΌμ΄μ λ₯Ό μ¬μ©νμ¬ μμκ°μ ν΄λΉνλ μΌμ΄μ€λ₯Ό μμ±ν μ μλ€.
μμκ° νμ μΌλ‘ μ΄κ±°νμ μ μνλ€λ©΄ μ΄κ±°νμ μμκ°μ νμ (rawValue λΌλ νλΌλ―Έν°)μ μ¬μ©νλ μ΄κΈ°νλ₯Ό
μλμΌλ‘ μμ νκ³ μ΄κ±°ν μΌμ΄μ€ λλ nil μ λ°ννλ€. μ΄ μ΄κΈ°νλ₯Ό μ΄μ©νμ¬ μ΄κ±°νμ μ μΈμ€ν΄μ€λ₯Ό λ§λ€ μ μλ€.
enum Direction: Int {
case north = 1
case south
case east
case west
}
if let direction = Direction(rawValue: 2) {
print("Direction: \(direction)") // μΆλ ₯: Direction: south
} else {
print("Invalid direction")
}
if let anotherDirection = Direction(rawValue: 5) {
print("Direction: \(anotherDirection)")
} else {
print("Invalid direction") // μΆλ ₯: Invalid direction
}
μ°κ΄λ κ°(Associated Values)μ μ΄κ±°νμ κ° μΌμ΄μ€μ νΉμ νμ μ κ°μ λλ°νμ¬ κ° μΌμ΄μ€κ° μΆκ°μ μΈ μ 보λ₯Ό κ°μ§ μ μλ€.
μ°κ΄λ κ°μ μ¬μ©νλ©΄ μ΄κ±°νμ κ° μΌμ΄μ€κ° μΆκ°μ μΈ μ 보λ₯Ό κ°μ§ μ μλ€.
λ€μν λ°μ΄ν° νμ μ μ μ₯νκ±°λ μ λ¬ν λ μ μ©νλ€.
enum Barcode {
case upc(Int, Int, Int, Int) //λ€ κ°μ Int νμ
μ μ°κ΄λ κ°μ κ°μ§
case qrCode(String) //νλμ String νμ
μ μ°κ΄λ κ°μ κ°μ§
}
//μ΄ μ΄κ±°νμ μ¬μ©νμ¬ μ€μ κ°μ μμ±νκ³ μ¬μ©νλ λ°©λ²
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("AFHDFDG") // qrCode μΌμ΄μ€λ‘ λ³κ²½νκ³ , String κ°μ μ°κ΄λ κ°μΌλ‘ μ μ₯
//μ΄κ±°ν μΌμ΄μ€λ₯Ό μ¬μ©νμ¬ μ°κ΄λ κ°μ μΆμΆνλ λ°©λ² :switch ꡬ문과 let ν€μλλ₯Ό μ¬μ©νμ¬ μ°κ΄λ κ° μΆμΆ
switch productBarcode{
case .upc(let numberSystem, let manufacturer, let product, let check):
print("UPC: \(numberSystem), \(manufacturer), \(product), \(check)")
case .qrCode(let productCode):
print("QR Code: \(productCode)")
//productBarcodeκ° qrCode μΌμ΄μ€μΌ λ, μ΄ μΌμ΄μ€κ° ν¬ν¨νκ³ μλ String κ°μ
//productCodeλΌλ λ³μμ ν λΉνλ€.
//productBarcode = .qrCode("ABCDEFGHIJ")μ΄λ©΄, productCodeλ "ABCDEFGHIJ"κ° λλ€.
}
enum HttpResponse {
case success(Int, String) //μν μ½λ(Int)μ λ©μμ§(String)
case failure(Int, String) //failure: μν μ½λ(Int)μ μλ¬ λ©μμ§(String)
}
let response = HttpResponse.success(200, "OK")
switch response {
case .success(let code, let message):
print("Success - Code: \(code), Message: \(message)")
case .failure(let code, let errorMessage):
print("Failure - Code: \(code), Error: \(errorMessage)")
}
μ°κ΄λ κ°κ³Ό CaseIterableμ ν¨κ» μ¬μ©ν μλ μλ€.
CaseIterableμ μ΄κ±°νμ λͺ¨λ μΌμ΄μ€λ₯Ό λ°°μ΄λ‘ μ 곡νμ§λ§ μ°κ΄λ κ°μ΄ μλ κ²½μ°
κ° μΌμ΄μ€κ° κ³ μ μ κ°μ κ°μ§ μ μκΈ° λλ¬Έμ μλμΌλ‘ λͺ¨λ μΌμ΄μ€λ₯Ό λμ΄ν μ μλ€.
μ΄κ±°ν μ¬μ© μ μμκ°κ³Ό μ°κ΄κ°μ λμμ μ¬μ©ν μ μμ.
π Reference
https://bbiguduk.gitbook.io/swift/language-guide-1/enumerations
[ Swift κΈ°μ΄ λ¬Έλ² ] ν΄λμ€ μμ (0) | 2024.06.04 |
---|---|
[ Swift κΈ°μ΄ λ¬Έλ² ] ν΄λμ€(Class)μ ꡬ쑰체(Struct) (0) | 2024.05.19 |
[ Swift κΈ°μ΄ λ¬Έλ² ] 컬λ μ (Collection) - Array, Dictionary, Set (0) | 2024.05.14 |
[ Swift κΈ°μ΄ λ¬Έλ² ] νν(Tuple) (0) | 2024.05.11 |
[ Swift κΈ°μ΄ λ¬Έλ² ] μ°μ°μ(Operator) (0) | 2024.05.09 |