๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[Swift] Enum์ด๋ž€?(feat. ์จ์•ผํ•˜๋Š” ์ด์œ )

by Fomagran ๐Ÿ’ป 2021. 4. 28.
728x90
๋ฐ˜์‘ํ˜•

 

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์š”์ฆ˜ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•œ ๊ณ ๋ฏผ์„ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์–ด๋–ค ๊ธ€์—์„œ ์ข‹์€ ์ฝ”๋“œ์˜ ๊ธฐ์ค€์ด

 

1. ๋ณด๊ธฐ ์‰ฌ์šด ๊น”๋”ํ•œ ์ฝ”๋“œ

 

2. ํšจ์œจ์ ์ธ ์ฝ”๋“œ

 

๋ผ๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”.

 

์—ด๊ฑฐํ˜•์€ ๋ฏธ๋ฆฌ ์ž์ฃผ ์“ฐ์ผ ํ‚ค์›Œ๋“œ๋ฅผ ์—ด๊ฑฐํ•ด๋†“๋Š” ๊ฒƒ์ธ๋ฐ ์ด๊ฒƒ์€ ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ ํฐ ๋„์›€์„ ์ฃผ๋”๋ผ๊ตฌ์š”.

 

๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ Enum์œผ๋กœ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๋Š”์ง€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Enum์ด๋ž€?

 

"์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์—ด๊ฑฐํ˜•์€ ์š”์†Œ, ๋ฉค๋ฒ„๋ผ ๋ถˆ๋ฆฌ๋Š” ๋ช…๋ช…๋œ ๊ฐ’์˜ ์ง‘ํ•ฉ์„ ์ด๋ฃจ๋Š” ์ž๋ฃŒํ˜•์ด๋‹ค. ์—ด๊ฑฐ์ž ์ด๋ฆ„๋“ค์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๋‹น ์–ธ์–ด์˜ ์ƒ์ˆ˜ ์—ญํ• ์„ ํ•˜๋Š” ์‹๋ณ„์ž์ด๋‹ค." - ์œ„ํ‚ค ๋ฐฑ๊ณผ -

 

๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ์ƒ์ˆ˜ ์—ญํ• ์„ ๊ฐ’๋“ค์„ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ๋‚˜์—ดํ•ด๋†“๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

Enum์„ ์“ฐ๋ฉด ๋ญ๊ฐ€ ์ข‹์€๋ฐ?๐Ÿคจ

 

1. ์ฝ”๋“œ๊ฐ€ ๋ณด๊ธฐ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค.

 

2. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ• ๋•Œ ์‹ค์ˆ˜๋ฅผ ์ค„์—ฌ์ค€๋‹ค.

 

3. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ํŽธ๋ฆฌํ•ด์ง„๋‹ค.

 

์ด ์„ธ๊ฐ€์ง€๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ์ด ์„ธ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~


1. ์ฝ”๋“œ๊ฐ€ ๋ณด๊ธฐ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค.

 

Enum์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฏธ๋ฆฌ ์›ํ•˜๋Š” ์ง‘ํ•ฉ์„ ๋‚˜์—ดํ•ด๋†“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐ ๋Œ€๋ฅ™์•ˆ์— ๋‚˜๋ผ๋“ค์ด ์žˆ๋Š” ๊ฒƒ์„ Enum์œผ๋กœ ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š”!

 

//case๋ฅผ ํ•œ์ค„๋กœ ์ ์–ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค!
enum Asia {
    case korea,china,japan
}

enum America {
    case usa
    case canada
    case mexico
}

enum Africa {
    case libya
    case egypt
    case ethiopia
}

enum Europe {
    case france
    case italy
    case germay
}

enum Oceania {
    case australia
    case newzealand
    case solomon
}

 

์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด ๊ฐ ๋Œ€๋ฅ™ ์•ˆ์— ์–ด๋–ค ๋‚˜๋ผ๋“ค์ด ์žˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋˜ํ•œ ์›ํ•˜๋Š” ๋‚˜๋ผ๋ฅผ ์ •ํ• ๋•Œ๋„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

let korea = Asia.korea

 

์•„๋ž˜์™€ ๊ฐ™์ด enum์— String์„ ์ฑ„ํƒํ•ด์ค€๋’ค ์›ํ•˜๋Š” ์ด๋ฆ„์„ ์ •ํ•ด์ฃผ๊ณ 

 

enum Asia:String {
    case korea = "๋Œ€ํ•œ๋ฏผ๊ตญ"
    case china = "์ค‘๊ตญ"
    case japan = "์ผ๋ณธ"
}

 

rawValue๋ฅผ ํ™œ์šฉํ•ด์„œ ์ •ํ•ด๋†“์€ ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

let korea = Asia.korea.rawValue
print(korea) //๋Œ€ํ•œ๋ฏผ๊ตญ

 

์ง€๊ตฌ๋ผ๋Š” enum์„ ๋งŒ๋“ค๊ณ  ๊ฐ ๋Œ€๋ฅ™๋ณ„ ๋‚˜๋ผ ์ด๋ฆ„์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋„๋ก ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

enum Earth{
    case Asia(country:Asia)
    case America(country:America)
    case Africa(country:Africa)
    case Europe(country:Europe)
    case Oceania(country:Oceania)
}

 

switch case ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์›ํ•˜๋Š” ๋‚˜๋ผ์ด๋ฆ„์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

var earth = Earth.Asia(country: .korea)

switch earth {
case .Asia(let country):
    print(country.rawValue)
default:
    print("default")
}

//๋Œ€ํ•œ๋ฏผ๊ตญ

2. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ• ๋•Œ ์‹ค์ˆ˜๋ฅผ ์ค„์—ฌ์ค€๋‹ค.

 

enum์„ ์“ฐ์ง€ ์•Š๊ณ  ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋Œ€๋ฅ™๋ณ„ ๋‚˜๋ผ ์ด๋ฆ„์„ ์ €์žฅํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

var countryDic:[String:[String]] = [:]

countryDic["Asia"] = ["korea","china","japan"]
countryDic["America"] = ["usa","canada","mexico"]
countryDic["Africa"] = ["egypt","lybia","ethiopia"]
countryDic["Europe"] = ["france","italy","germany"]
countryDic["Oceania"] = ["australia","newzealand","solomon"]

 

์—ฌ๊ธฐ์„œ ์•„์‹œ์•„์— ์žˆ๋Š” ํ•œ๊ตญ๊ฐ’์„ ์›ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ์— Asia๋ฅผ ํ‚ค๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  0๋ฒˆ์งธ ๊ฐ’์„ ๊บผ๋‚ด์™€์•ผ ํ• ๊ฒƒ์ž…๋‹ˆ๋‹ค.

let korea = countryDic["Asia"]![0]
print(korea) //korea

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ํ‚ค ๊ฐ’์„ ์ž˜๋ชป์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ๋‚˜๋ผ์˜ ๊ฐฏ์ˆ˜๋ณด๋‹ค ์ดˆ๊ณผํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

 

let korea = countryDic["asia"]![0] //Error
let korea = countryDic["Asia"]![5] //Error

 

ํ•˜์ง€๋งŒ enum์„ ์‚ฌ์šฉํ•ด์„œ ๋ฏธ๋ฆฌ ๋‚˜์—ดํ•ด๋†“์•˜๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด enum์— .์„ ๋ถ™์ด๋ฉด ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๊ณ  ์ž๋™์œผ๋กœ ์™„์„ฑ๋˜๊ฒŒ ๋•Œ๋ฌธ์—

 

์˜คํƒ€๋ฅผ ๋‚ผ ์ผ๋„ ์—†๊ณ  ์—†๋Š” ๋‚˜๋ผ๋ฅผ ์ž…๋ ฅํ•  ์ผ๋„ ์—†์–ด์ง‘๋‹ˆ๋‹ค.

 

 

 


3. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ํŽธ๋ฆฌํ•ด์ง„๋‹ค.

 

์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด Enum์œผ๋กœ ๋ฏธ๋ฆฌ ๊ฐ’์„ ๋‚˜์—ดํ•ด๋†“์œผ๋ฉด ์ง์ ‘ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•  ํ•„์š”์—†์ด .๊ณผ enterํ‚ค๋กœ ๊ฐ’์„ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ˆ˜๋„ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๋ฉด ๋‚˜๋ผ ์ด๋ฆ„์„ ์•Œ๋ ค์ฃผ๋Š” enum๊ณผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ฒ ์ž๊ฐ€ ์–ด๋ ต๊ณ  ๊ธด ๋ง๋ ˆ์ด์‹œ์•„ ์ˆ˜๋„ ์ฟ ์•Œ๋ผ๋ฃธํ‘ธ๋ฅด๋ฅผ ์˜ˆ์‹œ๋กœ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

 

enum Capital:String {
    case KualaLumpur = "Malaysia"
}

let capital = ["KualaLumpur":"Malaysia"]

 

์•„๋ž˜ GIF๋ฅผ ๋ณด์‹œ๋ฉด enum์„ ์‚ฌ์šฉํ•˜์˜€์„๋•Œ ์–ผ๋งˆ๋‚˜ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์—ด๊ฑฐํ˜•์ด ๋ฌด์—‡์ธ์ง€์™€ ์—ด๊ฑฐํ˜•์„ ์“ฐ๋ฉด ์–ด๋–ค ์ ์ด ์ข‹์€์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

 


Reference

 

 

Enumerations — The Swift Programming Language (Swift 5.4)

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

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€