Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift Documents

[Swift] ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ (8) - Enumerations (์—ด๊ฑฐํ˜•)

๊ฐ์ž ๐Ÿฅ” 2022. 2. 17. 19:21
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์ž์ž…๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ enumerations์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. swift ๊ฐœ๋ฐœ์ž ๋‹จํ†ก๋ฐฉ์—์„œ ๋งˆ์นจ ์–ด๋–ค๋ถ„์ด enum์— ๋Œ€ํ•ด์„œ ์‹ค๋ฌด์—์„œ ๋งŽ์ด ์“ฐ์ด๋Š”์ง€ ์—ฌ์ญค๋ดค๋Š”๋ฐ์š”.. (๋‚˜์ด์Šค ํƒ€์ด๋ฐ)

์‹ค๋ฌด์—์„œ๋„ ๊ต‰์žฅํžˆ ๋งŽ์ด ์“ฐ์ธ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹ค๋ฌด์—์„œ ์–ด๋–ป๊ฒŒ ์“ฐ์ด๋Š”์ง€๋Š” ๋‹ค์‹œ๋‹ค์‹œ! ๊ณต๋ถ€ํ•ด๋ณด๋ฉด์„œ ์‚ดํŽด๋ด์•ผ๊ฒ ์ง€๋งŒ, ๊ทธ๋ฆฌ๊ณ  ๊ฒฝํ—˜ํ•ด๋ด์•ผ๊ฒ ์ง€๋งŒ swift์˜ enum์€ ๋‹ค๋ฅธ ์–ธ์–ด์— ๋น„ํ•ด ๊ฐ•๋ ฅํ•œ ์ ์ด ๋งŽ๋‹ค๋ณด๋‹ˆ ์‹ค๋ฌด์—์„œ ํ•„์ˆ˜์ ์ด๋ผ๊ณ  ํ•˜๋„ค์š”. ์—ฌ๋Ÿฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด์•ผํ•  ๋•Œ๋‚˜, ๋ญ ๊ฐ€๋…์„ฑ๋ถ€๋ถ„์—์„œ๋„ ํฐ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹ค๋ฌด๋ฅผ ๊ฒฝํ—˜ํ•˜๊ธฐ ์ „์— enum์— ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ž˜ ์•Œ์•„๋ณด๊ณ  ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค.ใ…Žใ…Ž (๋‰ด๋น„์™€ ๊ณ ์ˆ˜์˜ ์ฐจ์ด๋Š” enum์„ ์–ผ๋งˆ๋‚˜ ์ž˜์“ฐ๋ƒ๋กœ ๊ฐˆ๋ฆฐ๋‹ค๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค ํ•˜ํ•˜ํ•˜ํ•ซ) 

์Šค์œ„ํ”„ํŠธ ์Šคํ„ฐ๋””๋ฅผ ํ•˜๋ฉด์„œ ์Šคํ„ฐ๋””์›๋ถ„์ด ์•„๋ž˜ ์˜์ƒ์„ ์ฐพ์•„์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. enumeration์„ ๊ณต๋ถ€ํ•˜๊ธฐ ์ „์—, ํ•ด๋‹น ์˜์ƒ์„ ๋ณด๋ฉด์„œ ์™œ ์‹ค๋ฌด์—์„œ enum์„ ์จ์•ผํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ด์œ ์™€ enum์ด ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

https://www.youtube.com/watch?v=CdBL7m1AeII 

 

๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

swift document chap.08 enumerations (์—ด๊ฑฐํ˜•)

 

Enumerations — The Swift Programming Language (Swift 5.6)

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. If you are familiar with C, you will know that C enumerations assign related names to a set of in

docs.swift.org

 

Enumerations

์—ด๊ฑฐํ˜•์€ ์—ฐ๊ด€์„ฑ์ด ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋ชจ์•„ ๋†“์€ ๊ฒƒ์„ ๋งํ•œ๋‹ค. C์–ธ์–ด์—์„œ์˜ ์—ด๊ฑฐํ˜•๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์—ด๊ฑฐํ˜• ์† ๊ฐ๊ฐ์˜ ๊ฐ’์— Intํƒ€์ž… ๊ฐ’์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. swift์—์„œ์˜ ์—ด๊ฑฐํ˜•์€ ์ข€ ๋” ์œตํ†ต์„ฑ์ด ์žˆ์–ด์„œ ์—ด๊ฑฐ์˜ ๊ฐ ๊ฒฝ์šฐ์— ๊ฐ’์„ ๊ผญ ์ œ๊ณตํ•  ํ•„์š”๋Š” ์—†๋‹ค. raw value๋ผ๊ณ  ์•Œ๋ ค์ง„ ๊ฐ ์ผธ์ด์Šค์˜ ๊ฐ’์€ String, Character, Int, Float, Double ๋“ฑ ์ผ ์ˆ˜ ์žˆ๋‹ค.

Swift์˜ ์—ด๊ฑฐํ˜•์€ ์ผ๊ธ‰๊ฐ์ฒด์ด๋‹ค. ํด๋ž˜์Šค์™€ ๋น„์Šทํ•˜๊ฒŒ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์›๋ž˜ ๊ตฌํ˜„๋œ ๊ธฐ๋Šฅ ์ด์ƒ์˜ ๊ฒƒ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ™•์žฅ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•œ๋‹ค. ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์„œ ํ‘œ์ค€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.


 

Enumeration Syntax

์—ด๊ฑฐํ˜•์„ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” enum ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์—ด๊ฑฐํ˜•์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

enum SomeEnumeration {
  // ์—ด๊ฑฐ์ฒด ์ •์˜๋Š” ์—ฌ๊ธฐ์— ๋‘ 
}

์—ด๊ฑฐํ˜•์„ ์„ ์–ธํ•ด๋ณด์ž.

enum CompassPoint {
  case north
  case south
  case east
  case west
}

//ํ•œ ์ค„๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
enum Planet {
  case mercury, venus, earth, mars, jupiter, saturn uranus, neptune
}

์œ„ ์ฝ”๋“œ์—์„œ ๋ณด์ด๋Š” north, south, east, west ๊ฐ€ enumeration case (์—ด๊ฑฐ ์ผ€์ด์Šค) ์ด๋‹ค. ๋” ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด case ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  north, south, east, west ๊ฐ’์€ ์•”์‹œ์ ์œผ๋กœ 0, 1, 2,,, ๊ณผ ๊ฐ™์ง€์•Š๋‹ค. ์—ด๊ฑฐ case๋“ค์€ ๊ทธ ์ž์ฒด๋กœ ๊ฐ’์ด๊ณ  ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜ํ•œ CompassPoint๋ผ๋Š” ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.

๊ฐ๊ฐ์˜ ์—ด๊ฑฐํ˜•์€ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์ •์˜ํ•œ๋‹ค. ์—ด๊ฑฐํ˜•์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋ฉด ํ•˜๋‚˜์˜ ์ƒˆ๋กœ์šด ํƒ€์ž…์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ด๊ฑฐํ˜•์€ Swift ์˜ ์ด๋ฆ„ ๊ทœ์น™์— ๋”ฐ๋ผ ์ด๋ฆ„์„ ๊ผญ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋Œ€๋ถ€๋ถ„ ๋ณต์ˆ˜ํ˜• ๋ณด๋‹ค๋Š” ๋‹จ์ˆ˜ํ˜•์œผ๋กœ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•œ๋‹ค.

 

โ–ถ ์—ด๊ฑฐํ˜• ํ•˜๋‚˜์˜ case ์— ์ ‘๊ทผ

์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ CompassPoint ์—ด๊ฑฐํ˜•์˜ ํ•˜๋‚˜์˜ ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ดˆ๊ธฐํ™”๋ฅผ ํ• ๋•Œ๋Š” directionToHead ํƒ€์ž…์„ ์ž๋™์œผ๋กœ ์ถ”๋ก ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ ์„ ์–ธ ์ดํ›„ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ๋•Œ๋Š” ์งง์€ ์  ๊ตฌ๋ฌธ์œผ๋กœ ์„ค์ •๊ฐ€๋Šฅํ•˜๋‹ค.

//์ ‘๊ทผํ•˜์—ฌ ๋ณ€์ˆ˜ ์ •์˜
var directionToHead = CompassPoint.west

//์ดˆ๊ธฐํ™”
directionToHead = .east

 

Matching Enumeration Values With a Switch Statement

์—ด๊ฑฐํ˜•์€ switch ๊ตฌ๋ฌธ๊ณผ ํ•จ๊ป˜ ์“ฐ๋ฉด ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

directionToHead = .south
switch directionToHead {
case .north:
  print("Lots of planets have a north")
case .south:
  print("Watch out for penguins")
case .east:
  print("Where the sun rises")
case .west:
  print("Where the skies are blue")
}

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด directionToHead ๋Š” ์—ด๊ฑฐํ˜• ์ผ€์ด์Šค ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. switch ๊ตฌ๋ฌธ์˜ ์ผ€์ด์Šค๊ฐ€ CompassPoint ์—ด๊ฑฐํ˜• ์ผ€์ด์Šค๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— default ๊ตฌ๋ฌธ์€ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋งŒ์•ฝ ํ•˜๋‚˜๋ฅผ ๋นผ๊ณ  ์ž‘์„ฑํ•˜๋ ค๋ฉด default ๊ตฌ๋ฌธ์€ ํ•„์š”ํ•˜๋‹ค.


 

Iterating over Enumeration Cases

์—ด๊ฑฐํ˜•์—์„œ๋Š” ์‚ฌ์šฉํ•  ๋•Œ ์—ด๊ฑฐํ˜•์˜ ๋ชจ๋“  ์ผ€์ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๊ธฐ๋ณธ์ ์ธ ์ œ๊ณต์€ ์•„๋‹ˆ๊ณ , CaseIterable ์ด๋ผ๋Š” ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•ด์•ผ ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

/*--------Array๋กœ ๋ฐ˜ํ™˜๋˜๋Š” allCases--------------*/
enum Beverage: CaseIterable {
  case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\\(numberOfChoices) beverages available")
// "3 beverages available" ๋ฅผ ์ธ์‡„ํ•จ

/*--------for-in ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ์ ‘๊ทผํ•˜๊ธฐ--------------*/
for beverage in Beverage.allCases {
  print(beverage)
}
// coffee
// tea
// juice

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Beverage.allcases ๋ผ๊ณ  ์ž‘์„ฑํ•˜์—ฌ Beverage์˜ ๋ชจ๋“  case์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. allCases ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด case๋“ค์ด Arrayํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ์ฝ”๋“œ์—์„œ count ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ Array์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ–ˆ๋‹ค.

๋˜ํ•œ for-in ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋“  case์— ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.


 

Associated Values

์ง€๊ธˆ๊นŒ์ง€์˜ ์˜ˆ์‹œ๋Š” ๋ชจ๋‘ ์—ด๊ฑฐํ˜• ์ผ€์ด์Šค๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜๋˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์„ค๋ช…ํ–ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ •์˜๋œ ์—ด๊ฑฐํ˜•์„ ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜์— Planet.earth ์™€ ๊ฐ™์ด ์ €์žฅํ•˜๊ณ  ๋‚˜์ค‘์— ์ด ๊ฐ’์„ ํ™•์ธํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ€๋”์€ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’๊ณผ ์—ด๊ฑฐํ˜• ์ผ€์ด์Šค ๊ฐ’์„ ํ•จ๊ป˜ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์—ด๊ฑฐํ˜•์˜ ์ผ€์ด์Šค์™€ ์ €์žฅ๋˜๋Š” ๋‹ค๋ฅธ ๊ฐ’์„ Associated values ๋ผ๊ณ  ํ•œ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ์žฌ๊ณ ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์‹œ์Šคํ…œ์ด ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๋ฐ”์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. ์ผ๋ถ€ ์ œํ’ˆ์€ 1D ๋ฐ”์ฝ”๋“œ๋ผ์„œ 0-9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์–ด๋–ค ์ œํ’ˆ์€ QR ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 2D ๋ฐ”์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ตœ๋Œ€ 2953๊ธธ์ด์˜ ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ์นœ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•  ๊ฒƒ์ธ๊ฐ€.

์ด์™€ ๊ฐ™์ด ๋‘๊ฐ€์ง€ ํƒ€์ž…์˜ ํ•œ๊ฐ€์ง€ ์ผ€์ด์Šค์— ์ €์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

enum Barcode {
  case upc(Int, Int, Int, Int)
  case qrCode(String)
}

์ด์ฒ˜๋Ÿผ upc ๋ฐ”์ฝ”๋“œ ์ผ€์ด์Šค๋Š” Int ํƒ€์ž…์„ 4๊ฐœ ๊ฐ€์ง„ ํŠœํ”Œ ํ˜•ํƒœ๋กœ, Qrcode ์ผ€์ด์Šค๋Š” stringํƒ€์ž…์œผ๋กœ ์ผ€์ด์Šค๋ฅผ ๋‚˜๋ˆ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ด๊ฑฐํ˜•์„ ๋งŒ๋“ค์–ด์ค€ ๋’ค ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ’์„ ํ• ๋‹นํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

var productBarcode = Barcode.upc(8, 85909, 51226, 3)

/*----- ์ดˆ๊ธฐํ™” -------*/
productBarcode = .qrCode( "ABCDEFGHIJKLMNOP")

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด switch ๊ตฌ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์„œ ํ•ด๋‹น ๊ฐ’์— ์žˆ๋Š” ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ ์ด๋Ÿฐ ์—ฌ๋Ÿฌ ๊ฐ’๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” Associated Value ๋ฅผ ์œ„ํ•ด ๊ฐ’๋“ค๋งˆ๋‹ค ๋ณ€์ˆ˜ ํ˜น์€ ์ƒ์ˆ˜๋กœ ์ •ํ•ด ์ด๋ฆ„์„ ๋ถ™์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

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).")
}
// "QR code: ABCDEFGHIJKLMNOP." ๋ฅผ ์ธ์‡„ํ•จ

/*------ let ์„ ํ•˜๋‚˜๋งŒ ๋‘˜ ์ˆ˜ ์žˆ๋‹ค. ------*/
/*------ ์œ„์™€ ๋™์ผํ•œ ์ฝ”๋“œ ๋‚ด์šฉ ์ž„ ------*/
switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
  print("UPC : \\(numberSystem), \\(manufacturer), \\(product), \\(check).")
case let .qrCode(productCode):
  print("QR code: \\(productCode).")
}

 

Raw Values

Associated values ์€ ์—ด๊ฑฐํ˜• ํƒ€์ž…์ด ์—ฌ๋Ÿฌ ํƒ€์ž…์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค๋ฉด, Raw Values๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋ฏธ๋ฆฌ ์„ ์–ธํ•ด ๋‘˜ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์ด๋‹ค.

enum ASCIIControlCharacter: Character {
  case tab = "\\t"
  case lineFeed = "\\n"
  case carriageReturn = "\\r"
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” ASCIIControlCharacter ๋ผ๋Š” ์—ด๊ฑฐํ˜•์˜ Raw ๊ฐ’์„ Character ๊ฐ’์œผ๋กœ ์ •์˜ํ–ˆ๋‹ค. ์ด ์™ธ์—๋„ Raw Values๋Š” Int, Float, Double, String ํƒ€์ž…์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ๊ฐ์˜ ์ผ€์ด์Šค ๋งˆ๋‹ค Raw Value๋Š” ๊ณ ์œ ํ•œ ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค.

Associated Values์™€์˜ ์ฐจ์ด์ 

Raw value ๋Š” ์—ด๊ฑฐํ˜•์„ ์ฒ˜์Œ ์ •์˜ํ•  ๋•Œ ๋ฏธ๋ฆฌ ์ฑ„์›Œ์ง„ ๊ฐ’์œผ๋กœ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ

Associated value๋Š” ํƒ€์ž…๋งŒ ์ •ํ•ด์ฃผ๊ณ  ๋‚˜์ค‘์— ๊ฐ’์„ ์ฑ„์šฐ๋Š” ๊ฒƒ

์ฆ‰, Raw value๋Š” ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ชจ๋‘ ๊ฐ™์€๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ, Associated value๋Š” ๋ณ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

Implicitly Assigned Raw Values (์•”์‹œ์ ์œผ๋กœ ํ• ๋‹นํ•˜๋Š” raw value)

์ •์ˆ˜๋‚˜ ๋ฌธ์ž์—ด ์›์‹œ ๊ฐ’์„ ์ €์žฅํ•œ ์—ด๊ฑฐํ˜•์„ ์ž‘์—…ํ•  ๋•Œ๋Š”, ๊ฐ case ์˜ ์›์‹œ ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ํ• ๋‹นํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋ฌผ๋ก  ์„ ์–ธํ•˜๋ฉด ๊ทธ ๊ฐ’์ด ํ•ด๋‹น case์˜ Raw Value๊ฐ€ ๋˜๊ฒ ์ง€๋งŒ, ์„ ์–ธํ•˜์ง€ ์•Š์œผ๋ฉด Swift๊ฐ€ ์•Œ์•„์„œ ๊ฐ’์„ ํ• ๋‹นํ•˜๊ฒŒ ๋œ๋‹ค.

enum Planet: Int {
  case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}

let earthsOrder = Planet.earth.rawValue
// earthsOrder is 3

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Planet.mercury ๋Š” 1 ์ด๋ผ๋Š” ๋ช…์‹œ์ ์ธ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ์ดํ›„๋Š” ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, ์•Œ์•„์„œ Swfit๊ฐ€ 2, 3, 4, 5, ... ๊ฐ’์„ ํ• ๋‹นํ•ด์ค€๋‹ค.

์›์‹œ๊ฐ’์œผ๋กœ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ• ๋•Œ๋Š”, ๊ทธ case ์ด๋ฆ„์— ์žˆ๋Š” ๋ฌธ์žฅ์ด ๊ฐ case ์˜ ์•”์‹œ์ ์ธ ๊ฐ’์ด๋‹ค. ์•„๋ž˜ ์—ด๊ฑฐํ˜• ์˜ˆ์‹œ๋ฅผ ๋ณด์ž.

enum CompassPoint: String {
  case north, south, east, west
}

let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is "west"

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์•„๋ฌด๊ฒƒ๋„ ์„ ์–ธํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด Case์˜ ์ด๋ฆ„์ด String ํƒ€์ž…์œผ๋กœ Raw Value ๊ฐ€ ๋œ๋‹ค.

 

Initializing from a Raw Value

์—ด๊ฑฐํ˜•์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— rawValue๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ํ•ด๋‹น Raw Value๊ฐ€ ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น case์˜ ๊ฐ’์ด ๋‚˜์˜ค๊ณ , ์กด์žฌํ•˜์ง€ ์•Š๋Š” Raw Value๋ผ๋ฉด nil ์ด ๋ฐ˜ํ™˜๋œ๋‹ค. ์ฆ‰, ์˜ต์…”๋„ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜์ด ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet ์€ Planet? ํƒ€์ž…์ด๊ณ  Planet.uranus ์™€ ๊ฐ™์Œ

์œ„์™€ ๊ฐ™์ด ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๋‹คํ–‰์ด์ง€๋งŒ, ์—†๋Š” ๊ฒฝ์šฐ nil ์ด ๋ฐ˜ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Š” ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋Š” ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด๋‹ค. (๊ฐ’์ด ์—†๋‹ค๋ฉด ์—๋Ÿฌ) ๋”ฐ๋ผ์„œ if let ๊ตฌ๋ฌธ์„ ์ด์šฉํ•œ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์•ˆ์ „ํ•˜๊ฒŒ ์ถ”์ถœํ•ด์•ผํ•œ๋‹ค.

let positionToFind = 11
if let somePlanet = Planet(rawValue: positionToFind) {
  switch somePlanet {
  case .earth:
    print("Mostly harmless")
  default:
    print("Not a safe place for humans")
  }
} else {
  print("There isn't a planet at position \\(positionToFind)")
}
// "There isn't a planet at position 11" ๋ฅผ ์ธ์‡„ํ•จ

์œ„ ์ฝ”๋“œ์—์„œ 11๋จผ์งธ ํ–‰์„ฑ์„ ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋ฉด Raw Value ๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— if let ๊ตฌ๋ฌธ์—์„œ else ๋ถ€๋ถ„์ด ์ˆ˜ํ–‰๋˜์—ˆ๋‹ค. (nil์„ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค)


 

Recursive Enumerations

์žฌ๊ท€ ์—ด๊ฑฐํ˜•์€ ์–ด๋–ค ์—ด๊ฑฐํ˜•์˜ ์ผ€์ด์Šค์— Associated Value์˜ ํƒ€์ž…์œผ๋กœ ์ž์‹ ์˜ ์—ด๊ฑฐํ˜• ํƒ€์ž…์ด ๋“ค์–ด๊ฐ„ ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ž์‹ ์˜ ์—ด๊ฑฐํ˜• ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ์ผ€์ด์Šค ์•ž์— indirect๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

enum ArithmeticExpression {
  case number(Int)
  indirect case addition(ArithmeticExpression, ArithmeticExpression)
  indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}

์œ„์ฝ”๋“œ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ๋ฒˆ ์“ฐ๊ธฐ ๊ท€์ฐฎ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ œ์ผ ์ฒ˜์Œ์— ํ•œ ๋ฒˆ๋งŒ ์จ์ค˜๋„ ๋œ๋‹ค.

indirect enum ArithmeticExpression {
  case number(Int)
  case addition(ArithmeticExpression, ArithmeticExpression)
  case multiplication(ArithmeticExpression, ArithmeticExpression)
}

์œ„ ์˜ˆ์ œ์—์„œ๋Š” addition, multiplication ์ผ€์ด์Šค์—์„œ recursive enumeration์ด ์‚ฌ์šฉ๋๊ณ , ํ•ด๋‹น ์ผ€์ด์Šค์—์„œ๋Š” associated valueํƒ€์ž…์œผ๋กœ ์ž์‹ ์˜ ์—ด๊ฑฐํ˜• ํƒ€์ž…์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋Š” (5 + 4) * 2 ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋Š” ์žฌ๊ท€์—ด๊ฑฐํ˜•์ธ ArithmeticExpression ์„ ์ž‘์„ฑํ•ด ๋ณด์•˜๋‹ค. ์žฌ๊ท€ ์—ด๊ฑฐํ˜•์€ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))

์žฌ๊ท€์—ด๊ฑฐํ˜•์€ ์‹ค์ œ ์žฌ๊ท€ ํ•จ์ˆ˜์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์‚ฐ์ˆ  ํ‘œํ˜„์‹์„ ํ‰๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

func evaluate(_ expression: ArithmeticExpression) -> Int {
  switch expression {
  case let .number(value):
    return value
  case let .addition(left, right):
    return evaluate(left) + evaluate(right)
  case let .multiplication(left, right):
    return evaluate(left) * evaluate(right)
  }
}

print(evaluate(product))
// "18" ๋ฅผ ์ธ์‡„ํ•จ
๋ฐ˜์‘ํ˜•