๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS ๐ŸŽ/Swift

Swift-06 ์—ด๊ฑฐํ˜•

by yongmin.Lee 2021. 2. 1.

Enumerations

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

์—ด๊ฑฐํ˜•์ด๋ž€?

- ์œ ์‚ฌํ•œ ์ข…๋ฅ˜์˜ ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ ๊ณณ์— ๋ชจ์•„์„œ ์ •์˜ํ•œ ๊ฒƒ.

- enum ์ž์ฒด๊ฐ€ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…

- ๋Œ€๋ฌธ์ž ์นด๋ฉœ์ผ€์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฆ„์„ ์ •์˜.

- ๊ฐ case๋Š” ๊ทธ ์ž์ฒด๊ฐ€ ๊ณ ์œ ์˜ ๊ฐ’์ž…๋‹ˆ๋‹ค.(๊ฐ case์— ์ž๋™์œผ๋กœ ์ •์ˆ˜๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š์Œ)

- ๊ฐ case๋Š” ์†Œ๋ฌธ์ž ์นด๋ฉœ์ผ€์ด์Šค๋กœ ์ •์˜

- ๊ฐ case๋Š” ํ•œ ์ค„์— ๊ฐœ๋ณ„๋กœ๋„, ํ•œ ์ค„์— ์—ฌ๋Ÿฌ๊ฐœ๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

enum Weekday {
    case mon
    case tue
    case wed
    case thu, fri, sat, sun
}

var day: Weekday = Weekday.mon // ์—ด๊ฑฐํ˜• ํƒ€์ž…๊ณผ ์ผ€์ด์Šค๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉ๊ฐ€๋Šฅ
day = .tue // ํƒ€์ž…์ด ๋ช…ํ™•ํ•˜๋‹ค๋ฉด .์ผ€์ด์Šค๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ

print(day) // tue

switch day {
case .mon, .tue, .wed, .thu:
    print("ํ‰์ผ์ž…๋‹ˆ๋‹ค")
case Weekday.fri:
    print("๋ถˆ๊ธˆ ํŒŒํ‹ฐ!!")
case .sat, .sun:
    print("์‹ ๋‚˜๋Š” ์ฃผ๋ง!!")
// switch์˜ ๋น„๊ต๊ฐ’์— ์—ด๊ฑฐํ˜• ํƒ€์ž…์ด ์œ„์น˜ํ•  ๋•Œ ๋ชจ๋“  ์—ด๊ฑฐํ˜• ์ผ€์ด์Šค๋ฅผ ํฌํ•จํ•œ๋‹ค๋ฉด default๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค
}

 

์›์‹œ๊ฐ’, rawValue๋ž€?

- ์—ด๊ฑฐํ˜•์— ์ •์ˆ˜ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ๋ฉด C ์–ธ์–ด์˜ enum ์ฒ˜๋Ÿผ ์ •์ˆ˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

- rawValue์—๋Š” ์ž๋™์œผ๋กœ 1์ด ์ฆ๊ฐ€๋œ ๊ฐ’์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

- rawValue๋Š” case๋ณ„๋กœ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ ธ์•ผํ•ฉ๋‹ˆ๋‹ค.

- rawValue๋ฅผ ๋ฐ˜๋“œ์‹œ ์ง€๋‹ ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ๊ตณ์ด ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

- ์ •์ˆ˜ ํƒ€์ž… ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, Hashable ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” ๋ชจ๋“  ํƒ€์ž…์„ ์›์‹œ๊ฐ’์˜ ํƒ€์ž…์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅ
- ์—ด๊ฑฐํ˜•์˜ ์›์‹œ๊ฐ’ ํƒ€์ž…์ด String์ผ ๋•Œ, ์›์‹œ๊ฐ’์ด ์ง€์ •๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด case์˜ ์ด๋ฆ„์„ ์›์‹œ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

enum Fruit: Int {
    case apple = 0
    case grape = 1
    case peach // ์ž๋™์œผ๋กœ 2 ํ• ๋‹น
    // case mango = 0 // apple์˜ ์›์‹œ๊ฐ’์— ์ด๋ฏธ 0์œผ๋กœ ํ• ๋‹น๋ฌ์œผ๋ฏ€๋กœ mango ์ผ€์ด์Šค์˜ ์›์‹œ๊ฐ’์„ 0์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค
}

print("Fruit.peach.rawValue == \(Fruit.peach.rawValue)")
// Fruit.peach.rawValue == 2

enum School: String {
    case elementary = "์ดˆ๋“ฑ"
    case middle = "์ค‘๋“ฑ"
    case high = "๊ณ ๋“ฑ"
    case university // ์—ด๊ฑฐํ˜•์˜ ์›์‹œ๊ฐ’ ํƒ€์ž…์ด String์ผ ๋•Œ, ์›์‹œ๊ฐ’์ด ์ง€์ •๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด case์˜ ์ด๋ฆ„์„ ์›์‹œ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ
}

print("School.middle.rawValue == \(School.middle.rawValue)")
// School.middle.rawValue = ์ค‘๋“ฑ

print("School.university.rawValue == \(School.university.rawValue)")
// School.middle.rawValue = university

 

์›์‹œ๊ฐ’์„ ํ†ตํ•œ ์ดˆ๊ธฐํ™”

- ์˜ต์…”๋„์„ ์ด์šฉํ•˜๋ฉด ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ• ๋•Œ ์›์‹œ๊ฐ’ ์ดˆ๊ธฐํ™”๋ฅผ ํ†ตํ•ด ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜ ์ƒ์„ฑ๊ฐ€๋Šฅ

- rawValue๊ฐ€ case์— ํ•ด๋‹นํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, rawValue๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐํ™” ํ•œ ์ธ์Šคํ„ด์Šค๋Š” ์˜ต์…”๋„ ํƒ€

enum Fruit: Int {
    case apple = 0
    case grape = 1
    case peach // ์ž๋™์œผ๋กœ 2 ํ• ๋‹น
}


//let apple: Fruit = Fruit(rawValue: 0) // ERROR
if let myFruit: Fruit? = Fruit(rawValue: 0) {
	print(myFruit) // apple
}

if let yourFruit: Fruit? = Fruit(rawValue: 3) {
	print(yourFruit) 
} // no print

 

์—ด๊ฑฐํ˜• ๋ฉ”์„œ๋“œ

- Swift์˜ ์—ด๊ฑฐํ˜•์—๋Š” ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ

enum Month {
    case dec, jan, feb
    case mar, apr, may
    case jun, jul, aug
    case sep, oct, nov
    
    func printMessage() {
        switch self {
        case .mar, .apr, .may:
            print("๋ด„")
        case .jun, .jul, .aug:
            print("์—ฌ๋ฆ„")
        case .sep, .oct, .nov:
            print("๊ฐ€์„")
        case .dec, .jan, .feb:
            print("๊ฒจ์šธ")
        }
    }
}

Month.mar.printMessage()