상세 컨텐츠

λ³Έλ¬Έ 제λͺ©

[ Swift 기초 문법 ] μ—΄κ±°ν˜•(Enumeration)

🍎 iOS/Swift

by AHN.Jihyeon 2024. 5. 17. 23:47

λ³Έλ¬Έ

 

 

 

πŸ”Ž  1 . μ—΄κ±°ν˜•(Enumeration)


: 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 μƒλž΅ κ°€λŠ₯
}
// 좜λ ₯κ°’: "남μͺ½"

 

 

 

πŸ”Ž  2. CaseIterable  


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. Raw Value(μ›μ‹œ κ°’)


μ›μ‹œκ°’μ€ 각 μΌ€μ΄μŠ€μ— 사전에 μ •μ˜λœ κ°’μœΌλ‘œ, 보톡 μ •μˆ˜, λ¬Έμžμ—΄, μ‹€μˆ˜ λ“±μ˜ κΈ°λ³Έ 데이터 νƒ€μž…μ„ μ‚¬μš©ν•œλ‹€.

 

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
}

 

 

 

 

πŸ”Ž 4. μ—°κ΄€λœ κ°’(Associated Values)


μ—°κ΄€λœ κ°’(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

 

κ΄€λ ¨κΈ€ 더보기