์ด๋ค ๊ทธ๋ฃน์ ์ฌ๋ฌ ์ผ์ด์ค๋ฅผ ๋ํ๋ธ๋ค.
enum CompassPoint {
case north
case south
case east
case west
}
, ๋ก ๊ตฌ๋ถํ ์ ์๋ค.
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
// ์๋ก์ด ๊ฐ์ ์ ์ํ ๋ ์ด์ฉํ ์ ์๊ณ ,
var directionToHead = CompassPoint.west
label.textColor = UIColor.white
// ํ์
์ ์ถ๊ฐ ๊ฐ๋ฅํ๋ฉด .๋ง ์ฐ์ด๋ ๋๋ค.
directionToHead = .east
switch ๊ตฌ๋ฌธ์ ์ด์ฉํด์ enum ์ผ์ด์ค์ ๋ฐ๋ผ ์ฒ๋ฆฌํ ์ ์๋ค.
switch ๋ฌธ์์๋ ๋ชจ๋ ์ผ์ด์ค๋ฅผ ๊ณ ๋ คํด์ผ ํ๋ฏ๋ก, ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐ์ง ํ์๊ฐ ์์ ๋, default ๋ฅผ ์ด์ฉํด์ ์ฒ๋ฆฌํ๋ค.
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")
}
// Prints "Watch out for penguins"
CaseIterable ์ ๋ฐ๋ฅด๊ฒ ํด์, enum ์ Iterable ํ๊ฒ ๋ง๋ค ์ ์๋ค.
์ฌ์ฉํ ๋๋, Case.allCases ๋ก ์ฌ์ฉํ๋ค. .count / for ๋ฌธ ... ๋ฑ Iterable ๊ฐ์ฒด์ ์ ์ฉ ๊ฐ๋ฅํ ๊ฒ๋ค ์ ์ฉ ๊ฐ๋ฅ.
enum Beverage: CaseIterable {
case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) beverages available")
// Prints "3 beverages available"
enum Size {
case large
case small
}
enum Menu {
case hamburger(String)
case fries(String, Size)
case beverage(String, Size)
}
switch ๋ฌธ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ.
var order = Menu.fries("cheese", .large)
switch order {
case .hamburger(let patties): // ์ฐ๊ด ๊ฐ์ ๊บผ๋ด ์ธ ์ ์๋ค.
print("\(patties) ํ๋ฒ๊ฑฐ ์
๋๋ค.")
case .fries(let source, let size):
print("\(source) ๊ฐ์ํ๊น \(size) ์ฌ์ด์ฆ ์
๋๋ค.")
case let .beverage(kind, size): // ์ ๋ถ let ์ผ๋ก ์ธ๊ฑฐ๋ฉด ์ด๋ ๊ฒ.
print("\(kind) \(size) ์ฌ์ด์ฆ ์
๋๋ค.")
}
enum ์ผ์ด์ค๋ง๋ค ๊ฐ์ ๋ ์ ์๋ค.
๋์ ๊ฐ ์ผ์ด์ค๋ง๋ค ๋์ผํ ํ์
์ ๊ฐ์ผ๋ก ์ฑ์์ ธ์ผ ํจ.
enum ASCIIControlCharacter: Character {
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
}
// Int ์ ๊ฒฝ์ฐ ์ฒซ๋ฒ์งธ ๊ฐ์ ์ฑ์ฐ๋ฉด ๋ค์์๋ 2 3 4 ... ๋ก ์์์ ์ฑ์์ง๋ค.
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
// String ์ ๊ฒฝ์ฐ ํน๋ณํ ์ง์ ํ์ง ์์ผ๋ฉด, ์ผ์ด์ค ์ด๋ฆ์ด raw value.
enum CompassPoint: String {
case north, south, east, west
}
let planetNum = Planet.earth.rawValue // rawValue ๋ก ์ ๊ทผํ ์ ์๋ค.
let possiblePlanet = Planet(rawValue: 7) // ๋ฐ๋๋ก ์์๊ฐ์ ์ด์ฉํด์ ์๋ ์ผ์ด์ค์ ์ ๊ทผํ ์ ์๋ค.
enum ์์ associated value ๋ฅผ ๋ค์ ๋ณธ์ธ์ ํ์ ์ธ enum ์ ๊ฐ์ง๋ ๊ฒฝ์ฐ.
enum ArithmeticExpression {
case number(Int)
// number + number
indirect case addition(ArithmeticExpression, ArithmeticExpression)
// number * number
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
// indirect ํค์๋๋ฅผ ์์ผ๋ก ๋นผ์ ์ธ ์๋ ์์.
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, 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))
// Prints "18"
enum Status {
case login
case signup
}
let status = Status.login
var urlString = ""
switch status {
case .login:
url = "https://localhost:8080/user/login"
case .signup:
url = "https://localhost:8080/user/signup"
}
์ฐธ๊ณ
https://bbiguduk.gitbook.io/swift/language-guide-1/enumerations