suvera-dev ๐Ÿฅฆ

Swift ) ๊ธฐ์ดˆ ๋ฌธ๋ฒ• 3์ผ์ฐจ - Enum ๋ณธ๋ฌธ

Language/Swift

Swift ) ๊ธฐ์ดˆ ๋ฌธ๋ฒ• 3์ผ์ฐจ - Enum

suvera 2022. 2. 4. 03:01

Swift ๊ธฐ์ดˆ๋ฌธ๋ฒ• 3์ผ์ฐจ Enum 

์‹ค์ œ ๊ฐ•์˜์—์„œ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์‹œ์ง€๋งŒ, ํ‰์†Œ์— ์ •๋ฆฌํ•ด๋‘๊ณ  ์‹ถ์—ˆ๋˜ ๋‚ด์šฉ๋“ค์„ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค !

 

๐Ÿœ Enum ์ด๋ž€ ? ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ์ƒ์ˆ˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ’๋“ค์„ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ๋‚˜์—ดํ•ด๋†“๋Š” ๊ฒƒ

+ ๊ฐ™์€ ์ฃผ์ œ๋กœ ์—ฐ๊ด€๋œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฉค๋ฒ„๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒํ˜• 

 

 

โ›ฑ ์—ด๊ฑฐํ˜•์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ? 

 

  • ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ , ์ฝ”๋“œ ๊ฐ€๋…์„ฑ๋„ ์ข‹์•„์ง€๊ณ , ์‹ค์ˆ˜๋ฅผ ์ค„์—ฌ์ค€๋‹ค ! 
  • ๋˜ํ•œ Heap์— ์ €์žฅ๋˜๋Š” String๊ณผ ๋‹ฌ๋ฆฌ, Enum์€ ๊ฐ’ ํ˜•์‹์œผ๋กœ Stack์— ์ €์žฅ๋˜์–ด ์„ฑ๋Šฅ๋ฉด์—์„œ์˜ ํ–ฅ์ƒ๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค ! 
  • ๊ณตํ†ต๋œ ์ฃผ์ œ์— ๋Œ€ํ•ด์„œ ์ด๋ฏธ ์ •ํ•ด๋†“์€ ์ž…๋ ฅ ๊ฐ’๋งŒ ์„ ํƒํ•ด์„œ ๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ !

 

 

 

 

1. ์›์‹œ ๊ฐ’์ด ์—†๋Š” ์—ด๊ฑฐํ˜• 

import UIKit

// ํ•™๊ต - ์ดˆ, ์ค‘, ๊ณ 
enum School {
//    case elementary
//    case middle
//    case high
    case elementary, middle, high
}

let yourSchool = School.high
//print("yourSchool: \(yourSchool)")
print("yourSchool: ", yourSchool)

 

์šฐ๋ฆฌ๊ฐ€ ์„ ์–ธํ•œ ์ด ์—ด๊ฑฐํ˜•์ด ํ•˜๋‚˜์˜ ์ž๋ฃŒํ˜•์ด ๋˜์–ด ์„ ์–ธํ•œ ์—ด๊ฑฐํ˜•์„ ํƒ€์ž…์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ณ , 

์—ด๊ฑฐํ˜•์œผ๋กœ ํƒ€์ž…์ด ์ง€์ •๋œ ๊ฒฝ์šฐ, .(์ ๋ฌธ๋ฒ•)์„ ์ด์šฉํ•ด ๋‚ด๊ฐ€ ์„ ์–ธํ•œ case์— ํ•œํ•ด์„œ๋งŒ ์ ‘๊ทผ !

=> ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์‹ค์ˆ˜๋ฅผ ์ค„์—ฌ์ค๋‹ˆ๋‹ค ! 

 

 

 

 

2. ์›์‹œ๊ฐ’์ด ์žˆ๋Š” ์—ด๊ฑฐํ˜• (rawValue)

enum Grade : Int {
    case zero     // 0
    case first    // 1
    case second   // 2
}

let yourGrade = Grade.second.rawValue
print("yourGrade : \(yourGrade)")

์—ด๊ฑฐํ˜•์ด ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ !  first ๋Š” 1 , second๋Š” 2

.rawValue๋กœ enum์ด ๊ฐ€์ง„ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค 

 

rawValue๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์€ 3๊ฐ€์ง€ ! enum ์„ ์–ธ ์‹œ ์ด๋ฆ„ ์˜†์— Type์„ ๊ผญ ๋ช…์‹œํ•ด์ฃผ์ž 

  • Number Type 
  • Character Type
  • String Type 

์œ„์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์ด Int๋ผ๋Š” ํƒ€์ž…์„ enum ์„ ์–ธ ์‹œ ์ด๋ฆ„ ์˜†์— ๋ช…์‹œํ•ด์ฃผ๋ฉด, ๊ฐ€์žฅ ๋จผ์ € ์„ ์–ธ๋œ case ๋ถ€ํ„ฐ 0๋ถ€ํ„ฐ 1์”ฉ ์ฆ๊ฐ€๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค !

 

 

 

enum Grade : Int {
    case first = 1
    case second = 2
}

์ง์ ‘ rawValue๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค ! 

rawValue๊ฐ€ ์—†๋Š” case๋Š” ๋ฐ”๋กœ ์ด์ „์˜ case์˜ rawValue์— +1 ํ•œ ๊ฐ’์œผ๋กœ ์…‹ํŒ…

 

 

 

 

enum Grade : Double {
    case first = 1.0
    // case second = 2.0 ๋…ธ๋…ธ
    case second = 2
    case third  // 3์œผ๋กœ ์…‹ํŒ…๋˜๊ณ  Double๋กœ ํ˜•๋ณ€ํ™˜๋˜์–ด ๋“ค์–ด๊ฐ ! 
}

๐Ÿ“Œ Intํ˜•์ด ์•„๋‹Œ ์ž๋ฃŒํ˜•์œผ๋กœ ํ–ˆ์„ ๊ฒฝ์šฐ์—, ๋ชจ๋“  case์— ๋Œ€ํ•ด ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ ! 

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ Raw Value ๊ฐ’์„ ์ง€์ •ํ•  ๋•Œ ์ •์ˆ˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ˆ˜ ๊ฐ’์—๋Š” ๋ชป ๋”ํ•œ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค ! 

Raw Value๋ฅผ ์ƒ๋žตํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๋ฐ”๋กœ ์ด์ „ case์˜ Raw Value๋ฅผ ์ •์ˆ˜ ๊ฐ’์œผ๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค 

 

  • ๊ฐ™์€ ๋งฅ๋ฝ์œผ๋กœ, Character Type์œผ๋กœ ์—ด๊ฑฐํ˜•์„ ์„ ์–ธํ•  ๊ฒฝ์šฐ ๋ชจ๋“  case์— ๋Œ€ํ•œ Raw Value๋ฅผ ์ง์ ‘ ์„ ์–ธํ•ด์ค˜์•ผํ•œ๋‹ค !
  • String์€ Character์™€ ๋‹ฌ๋ฆฌ Raw Value๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, case ์ด๋ฆ„๊ณผ ๋™์ผํ•œ Raw Value๊ฐ€ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.

 

 

 

๐Ÿ–Œ Raw Value๊ฐ€ ์žˆ๋Š” ์—ด๊ฑฐํ˜•์˜ ๊ฒฝ์šฐ, rawValue๋กœ ์—ด๊ฑฐํ˜• ์ƒ์„ฑ ๊ฐ€๋Šฅ

- ์—†๋Š” Raw Value์ผ ๊ฒฝ์šฐ nil ๋ฆฌํ„ด 

var school = SchoolWithRawValue.init(rawValue: "high")

 

 

ํ•˜์ง€๋งŒ ... 

์œ„์™€ ๊ฐ™์ด RawValue๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, 

๋ชจ๋“  case๊ฐ€ ๋™์ผํ•œ ํ˜•์‹์œผ๋กœ Raw Value๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๊ณ  case ๋ณ„ ๊ฐ’์€ ๋ฏธ๋ฆฌ ์ง€์ •๋œ ํ•œ ๊ฐ€์ง€ ๊ฐ’๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ 

 

 

 

 

๐ŸŒ€ Associated Values(์—ฐ๊ด€๊ฐ’)

- ํŠœํ”Œ์„ ํ™œ์šฉํ•ด์„œ, ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์—ฐ๊ด€๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ์„ ์–ธํ•ด์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ์š” ! 

- SchoolDetail์ด๋ผ๋Š” enum์„ ๋งŒ๋“ค๊ณ  ๊ฐ ํ•™๊ต์˜ ์ด๋ฆ„์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

- ์—ด๊ฑฐํ˜• ์ƒ์„ฑ ์‹œ ๋‚ด๊ฐ€ ๋งŒ๋“ค์–ด๋‘” case๊ฐ€ ์—ฐ๊ด€๊ฐ’์ด ํ•จ๊ป˜ ๋œจ๊ณ , ์—ด๊ฑฐํ˜•์— ์ง์ ‘ ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค 

- switch๋ฌธ๊ณผ ๋งค์นญ ์‹œ์ผœ์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ (๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ)

enum SchoolDetail {
    case elementary(name: String)
    case middle(name: String)
    case high(name: String)
    
    func getName() -> String {
        switch self {
        case .elementary(let name):
            return name
        case let .middle(name):
            return name
        case .high(let name):
            return name
        }
    }
}

let yourMiddleSchoolName = SchoolDetail.middle(name: "์ •๋Œ€๋ฆฌ์ค‘ํ•™๊ต")

print("yourMiddleSchoolName: \(yourMiddleSchoolName.getName())")

 

+ ์ถ”๊ฐ€์ ์œผ๋กœ Class, Struct์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, Enum์—์„œ๋„ ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•  ์ˆ˜ ์žˆ๋‹ค

CaseIterable์ฒ˜๋Ÿผ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•  ์ˆ˜ ์žˆ๊ณ , ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์— ์ง€์ •๋œ allCases ์‚ฌ์šฉ๊ฐ€๋Šฅ ! 

 

+ ๊ธฐํƒ€ ์˜ˆ์‹œ๋“ค ! 

// ์ถ”๊ฐ€์ ์œผ๋กœ ์ฐพ์•„๋ณธ ์˜ˆ์‹œ๋“ค

enum AppleProduct {
    case iPad(model: String)
    case iPhone(model: String, storage: Int)
    case macbook(model: String, storage: Int, size: Int)
}

let product: AppleProduct = .iPhone(model: "8", storage: 64)

// ์—ฐ๊ด€๊ฐ’์„ ๊ฐ€์ง€๋Š” ์—ด๊ฑฐํ˜•์˜ switch ๋งค์นญ

switch product {
case .iPad("5s"): break // ์—ฐ๊ด€๊ฐ’์ด 5s๋ฉด ๋งค์นญ
case .iPad: break // ์—ฐ๊ด€๊ฐ’ ๋ฌด์‹œ
case .iPhone("X",_): break // ์—ฐ๊ด€๊ฐ’ ์ƒ๋žต ๊ฐ€๋Šฅ
case .iPhone(let model,var storage): break // ์—ฐ๊ด€๊ฐ’ ์ƒ์ˆ˜(๋ณ€์ˆ˜) ๋ฐ”์ธ๋”ฉ
case let .macbook(model, storage, size): break // ๋ชจ๋“  ์—ฐ๊ด€๊ฐ’์„ let์œผ๋กœ ๋ฐ”์ธ๋”ฉ ์‹œ let์„ ๋งจ ์•ž์œผ๋กœ ๋บผ ์ˆ˜ ์žˆ์Œ
}

// ์—ฐ๊ด€๊ฐ’์„ ๊ฐ€์ง€๋Š” ์—ด๊ฑฐํ˜•์˜ if ์‚ฌ์šฉ๋ฒ•

if case let .iPhone("8", storage) = product {
    // product์˜ ์ฒซ๋ฒˆ์งธ ์—ฐ๊ด€๊ฐ’์ด "8" ์ด๋ฉด ๋งค์นญ
    print(storage)
}

if case .iPhone(_,64) = product {
    // product์˜ ์ฒซ๋ฒˆ์งธ ์—ฐ๊ด€๊ฐ’์€ ์ƒ๊ด€์—†๊ณ , ๋‘๋ฒˆ์งธ ์—ฐ๊ด€๊ฐ’์ด 64๋ฉด ๋งค์นญ
    print("iPhone 64GB")
}

// ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” ์—ด๊ฑฐํ˜•
enum AppleProduct2: String, CaseIterable {
    case iPad = "5, 128"
    case iPhone = "6, 64"
    case macbook = "Pro, 256"
}

AppleProduct2.allCases.randomElement()

 

 

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ ! 

 

Swift) ์—ด๊ฑฐํ˜•(Enumeration) ์‹ฌํ™”ํŽธ

์•ˆ๋…•ํ•˜์„ธ์š”, ์†Œ๋“ค์ž…๋‹ˆ๋‹ค :) ์š”์ฆ˜ ํฌ์ŠคํŒ…์„ ์ž˜ ์•ˆํ•˜๊ฒŒ ๋˜๋Š” ๊ฑฐ ๊ฐ™์•„์š” ์ธ์ƒ ์š”์ฆ˜ ์ œ ๋ชจํ† ๋Š” ์ •๋ง ํ•˜๊ธฐ ์‹ซ์€ ์ผ์ด ์žˆ์„ ๋•Œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ทธ ์ผ์„ ๋นจ๋ฆฌ ํ•ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด.. ์ €๋„

babbab2.tistory.com

 

Comments