[Swift๐Ÿฆฉ] #8 ์—ด๊ฑฐํ˜•

๋˜์ƒยท2022๋…„ 3์›” 25์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
20/42
post-thumbnail

1. Enum

์–ด๋–ค ๊ทธ๋ฃน์˜ ์—ฌ๋Ÿฌ ์ผ€์ด์Šค๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

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

2. Switch

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"

3. Iterating Cases

๐Ÿ“• ์—ด๊ฑฐํ˜•์˜ ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ์•Œ์•„์•ผํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

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"

4. Associated Values โญ๏ธ

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) ์‚ฌ์ด์ฆˆ ์ž…๋‹ˆ๋‹ค.")
}

5. Raw Values

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) // ๋ฐ˜๋Œ€๋กœ ์›์‹œ๊ฐ’์„ ์ด์šฉํ•ด์„œ ์›๋ž˜ ์ผ€์ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

6. Recursive Enum

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

profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€