λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

🍎 iOS/Swift

[Swift] μŠ€μœ„ν”„νŠΈ 데이터 νƒ€μž…(Swift data type) - 3. μ—΄κ±°ν˜• νƒ€μž… (enum)

1. Swift κΈ°λ³Έ 데이터 νƒ€μž…

   1 - 1. κΈ°λ³Έ λ°μ΄ν„° νƒ€μž… (Bool, Int, UInt, Float, Double, Character, String)

   1 - 2. Any, AnyObject, nil, Typealias

>>https://lxxyeon.tistory.com/23

 

[Swift] μŠ€μœ„ν”„νŠΈ 데이터 νƒ€μž…(Swift data type) - 1.κΈ°λ³Έ 데이터 νƒ€μž…, Any, AnyObject, nil, Typealias

πŸ’‘ Swift 데이터 νƒ€μž… νŠΉμ§• - λͺ¨λ“  데이터 νƒ€μž…μ˜ 이름은 첫 κΈ€μžλ₯Ό λŒ€λ¬Έμžλ‘œ μž…λ ₯ν•œλ‹€. - λͺ¨λ“  데이터 νƒ€μž…(Int, Float, Double, Bool, String, Array, Dictionary, Set λ“±)은 λͺ¨λ‘ Struct (ꡬ쑰체)λ₯Ό κΈ°..

lxxyeon.tistory.com

2. μ»¬λ ‰μ…˜ νƒ€μž… (Array, Dictionary, Set)

>> https://lxxyeon.tistory.com/140

 

[Swift] μŠ€μœ„ν”„νŠΈ 데이터 νƒ€μž…(Swift data type) - 2. μ»¬λ ‰μ…˜ νƒ€μž…(Array, Dictionary, Set)

1. Swift κΈ°λ³Έ 데이터 νƒ€μž…  1 - 1. κΈ°λ³Έ λ°μ΄ν„° νƒ€μž… (Bool, Int, UInt, Float, Double, Character, String)  1 - 2. Any, AnyObject, nil, Typealias 2. μ»¬λ ‰μ…˜ νƒ€μž… (Array, Dictionary, Set) 3. μ—΄κ±°..

lxxyeon.tistory.com

3. μ—΄κ±°ν˜• νƒ€μž… (enum)


🍎 μ—΄κ±°ν˜•(Enum)

  • μ—°κ΄€λœ ν•­λͺ©λ“€μ„ λ¬Άμ–΄μ„œ ν‘œν˜„ν•  수 μžˆλŠ” νƒ€μž…
  • Swift 만의 큰 강점!!! >> Cλ‚˜ Objective-Cκ°€ Integerκ°’λ“€λ‘œ μ—΄κ±°ν˜•μ„ κ΅¬μ„±ν•œ 것에 λ°˜ν•΄ Swiftμ—μ„œλŠ” case값이 string, character, integer, floting 값듀을 μ‚¬μš© κ°€λŠ₯
  • 각 caseλŠ” κ·Έ μžμ²΄κ°€ 고유의 κ°’
  • 각 caseλŠ” μ†Œλ¬Έμž μΉ΄λ©œμΌ€μ΄μŠ€λ‘œ μ •μ˜

πŸ’‘ μ—΄κ±°ν˜• 문법

enum <이름> {
    case <이름1>
    case <이름2>
    case <이름3>, <이름4>, <이름5>
    ...
}
//ex)
enum Weekday {
    case mon
    case tue
    case wed
    case thu, fri, sat, sun
}

πŸ’‘ μ—΄κ±°ν˜• κ°’ μ ‘κ·Ό

var day: Weekday = Weekday.mon
day 		//mon
day = .tue	// νƒ€μž…μ΄ λͺ…ν™•ν•˜λ‹€λ©΄ μΆ•μ•½ν•΄μ„œ μ‚¬μš© κ°€λŠ₯
print(day) 	// tue

πŸ’‘ Switchλ¬Έμ—μ„œ μ‚¬μš©

switch day {//μ—΄κ±°ν˜•
case .mon, .tue, .wed, .thu:
    // case ν•˜λ‚˜λΌλ„ μ—†λŠ” κ²½μš°λŠ” default κ΅¬ν˜„ν•΄μ•Όν•¨
    // λͺ¨λ“  case κ΅¬ν˜„μ‹œ default μž‘μ„±ν•  ν•„μš” μ—†λ‹€.
    print("ν‰μΌμž…λ‹ˆλ‹€")
case Weekday.fri:
    print("뢈금 νŒŒν‹°!!")
case .sat, .sun:
    print("μ‹ λ‚˜λŠ” 주말!!")
}

πŸ’‘ μ›μ‹œκ°’

  • νŠΉμ • νƒ€μž…μœΌλ‘œ μ§€μ •λœ 값을 κ°€μ§ˆ 수 μžˆλ‹€
  • C μ–Έμ–΄μ˜ enum처럼 μ •μˆ˜κ°’μ„ κ°€μ§ˆ 수 μžˆλ‹€
  • rawValue둜 μ •μˆ˜ κ°’ μ ‘κ·Ό κ°€λŠ₯
  • caseλ³„λ‘œ 각각 λ‹€λ₯Έ 값을 κ°€μ Έμ•Ό 함
enum Fruit: Int {//μ—΄κ±°ν˜• 이름 μ˜†μ— μ›μ‹œκ°’μ„ κ°€μ§ˆ νƒ€μž… μ„ μ–Έ
    case apple = 0
    case grape = 1//1 μ§€μ›Œλ„ μžλ™μœΌλ‘œ 1 ν• λ‹Ή
    case peach //1μ”© 증가 > peach = 2
//    case mango = 0//였λ₯˜ 발쌩
}

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

//μ •μˆ˜ νƒ€μž… 뿐만 μ•„λ‹ˆλΌ Hashable ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λŠ” λͺ¨λ“  νƒ€μž…μ΄ μ›μ‹œκ°’μ˜ νƒ€μž…μœΌλ‘œ 지정될 수 μžˆμŠ΅λ‹ˆλ‹€
enum School: String {
    case elementary = "μ΄ˆλ“±"
    case middle = "쀑등"
    case high = "κ³ λ“±"
    case university //rawvalue κ°’ : case 이름
}

print("School.middle.rawValue == \(School.middle.rawValue)")
// School.middle.rawValue == 쀑등

print("School.university.rawValue == \(School.university.rawValue)")
// School.university.rawValue == university //μΌ€μ΄μŠ€μ΄λ¦„κ°€μ Έμ˜΄

πŸ’‘ μ›μ‹œκ°’μ„ ν†΅ν•œ μΌ€μ΄μŠ€ μ΄ˆκΈ°ν™”

  • rawValueλ₯Ό 톡해 μ΄ˆκΈ°ν™” κ°€λŠ₯
  • rawValueκ°€ case에 ν•΄λ‹Ήν•˜μ§€ μ•Šμ„ 수 μžˆμœΌλ―€λ‘œ rawValueλ₯Ό 톡해 μ΄ˆκΈ°ν™” ν•œ μΈμŠ€ν„΄μŠ€λŠ” μ˜΅μ…”λ„ νƒ€μž…
enum Fruit: Int {
    case apple = 0
    case grape = 1
    case peach 
}
//let apple: Fruit = Fruit(rawValue: 0) // μΌλ°˜νƒ€μž…μ— ν• λ‹Ήν•˜λ €κ³  ν•˜λ©΄ 였λ₯˜λ°œμƒ
let apple: Fruit? = Fruit(rawValue: 2)  // μ˜΅μ…”λ„ νƒ€μž…

enum Numbers: Int{
    case zero
    case one=2
}
print("\(Numbers.zero.rawValue)")//0

var one = Numbers(rawValue: 2)    //rawValue 값을 가진 caseλŠ”?
//print("\(one)")//optional νƒ€μž…!
var one2: Numbers? = Numbers(rawValue: 0)

if let orange: Fruit = Fruit(rawValue: 5) {
    print("rawValue 5에 ν•΄λ‹Ήν•˜λŠ” μΌ€μ΄μŠ€λŠ” \(orange)μž…λ‹ˆλ‹€")
} else {
    print("rawValue 5에 ν•΄λ‹Ήν•˜λŠ” μΌ€μ΄μŠ€κ°€ μ—†μŠ΅λ‹ˆλ‹€")
} // rawValue 5에 ν•΄λ‹Ήν•˜λŠ” μΌ€μ΄μŠ€κ°€ μ—†μŠ΅λ‹ˆλ‹€

πŸ’‘ λ©”μ„œλ“œ μΆ”κ°€ κ°€λŠ₯

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()
// λ”°μŠ€ν•œ λ΄„~
728x90
λ°˜μ‘ν˜•