πŸ‘©πŸ»‍πŸ’» iOSun
article thumbnail

μ—΄κ±°ν˜• (Enumerations)

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

enum Planet {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
  • 콀마둜 κ΅¬λΆ„ν•˜μ—¬ ν•œμ€„λ‘œ ν‘œκΈ°ν•  수 μžˆλ‹€.

 

enum Beverage: CaseIterable {
    case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
  • μ—΄κ±°ν˜• 이름 뒀에 : CaseIterable 을 μž‘μ„± → allCases ν”„λ‘œνΌν‹°λŠ” array둜 κ΄€λ ¨ λ©”μ†Œλ“œ μ‚¬μš© κ°€λŠ₯

 

μ—°κ΄€λœ κ°’ (Associated Values)

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

var productBarcode = Barcode.upc(8, 85909, 51226, 3)
  • μΌ€μ΄μŠ€ κ°’κ³Ό ν•¨κ»˜ λ‹€λ₯Έ νƒ€μž…μ˜ 값을 μ €μž₯ν•  수 μžˆλŠ” 것

 

μ›μ‹œκ°’ (Raw Values)

enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
  • λ™μΌν•œ νƒ€μž…μ˜ κΈ°λ³Έκ°’ μ„€μ • κ°€λŠ₯
  • λͺ¨λ“  μΌ€μ΄μŠ€κ°€ μ›μ‹œκ°’μ„ κ°€μ§ˆ ν•„μš”λŠ” μ—†λ‹€.
  • λ‹€μŒ μΌ€μ΄μŠ€μ˜ μ›μ‹œκ°’μ— ν•˜λ‚˜μ”© 증가, 첫번째 μΌ€μ΄μŠ€μ— κ°’ 섀정이 μ•ˆλ˜μ–΄ 있으면 0 으둜 μ„€μ •ν•œλ‹€.

 

μ›μ‹œκ°’μœΌλ‘œ μ΄ˆκΈ°ν™” (Initializing from a Raw Value)

let possiblePlanet = Planet(rawValue: 7) // Planet.uranus
  • μ˜΅μ…”λ„ νƒ€μž…μž„ → rawValue에 ν•΄λ‹Ήν•˜λŠ” μΌ€μ΄μŠ€κ°€ μ—†λ‹€λ©΄ nil 리턴

 

μž¬κ·€ μ—΄κ±°ν˜• (Recursive Enumerations)

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)
  • ν•˜λ‚˜ μ΄μƒμ˜ μ—°κ΄€λœ κ°’μœΌλ‘œ μ—΄κ±°ν˜•μ˜ λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€λ₯Ό 가지고 μžˆλŠ” μ—΄κ±°ν˜•
  • μž¬κ·€μ μž„μ„ λ‚˜νƒ€λ‚΄κΈ° μœ„ν•΄ μΌ€μ΄μŠ€ μž‘μ„± 전에 indirect ν‚€μ›Œλ“œλ‘œ ν‘œμ‹œ
profile

πŸ‘©πŸ»‍πŸ’» iOSun

@iosun

ν¬μŠ€νŒ…μ΄ μ’‹μ•˜λ‹€λ©΄ "μ’‹μ•„μš”β€οΈ" λ˜λŠ” "κ΅¬λ…πŸ‘πŸ»" ν•΄μ£Όμ„Έμš”!

profile on loading

Loading...