곡λΆν λ΄μ©μ μ 리ν κΈμ λλ€.
λ°μ΄ν° νμ κ³ κΈ(3)
- μ΄κ±°ν
- κΈ°λ³Έ μ΄κ±°ν
- μμ κ°
- μ°κ΄ κ°
- νλͺ© μν
- μν μ΄κ±°ν
- λΉκ΅ κ°λ₯ν μ΄κ±°ν
1. μ΄κ±°ν
: μ°κ΄λ νλͺ©λ€μ λ¬Άμ΄μ ννν μ μλ νμ .
- νλ‘κ·Έλλ¨Έκ° μ μν΄μ€ νλͺ© κ° μΈμλ μΆκ°/μμ λΆκ°λ₯
- μ€μννΈμ μ΄κ±°νμ κ° νλͺ©μ΄ κ·Έ μμ²΄λ‘ κ³ μ μ κ°μ΄ λ μ μλ€. λ°λΌμ νλͺ©λ³ κ°μ κ°μ§ μλ μκ³ κ°μ§μ§ μμ μλ μλ€.
- κ° νλͺ©μ μμ κ°(Raw Value), μ¦ μ€μ κ°μ κ°μ§ μ μλ€.
- κ° νλͺ©μ μ°κ΄ κ°(Assosiated Value)λ₯Ό μ¬μ©ν΄ κ°μ λ¬Άμμ ꡬνν μ μλ€.
- μ£Όλ‘ switchλ¬Έμμ μ¬μ©λλ€.
- μ©λ
- μ ν΄μ§ μ νμ§λ₯Ό μ£Όκ³ μΆμ λ
- μ ν΄μ§ κ° μΈμλ μ λ ₯λ°κ³ μΆμ§ μμ λ
- μμλ μ λ ₯ κ°μ΄ νμ λμ΄ μμ λ
2. κΈ°λ³Έ μ΄κ±°ν
- enum ν€μλ ( enumerate: μ΄κ±°νλ€ )
- μ¬λ¬ μ€ λλ ν μ€λ‘ νλͺ©μ νν ν΄ μ€ μ μλ€.
μ΄κ±°νμ μ μΈ
//νλͺ© μ¬λ¬ μ€ νν
enum Fruit{
case apple
case orange
case banana
}
//νλͺ© ν μ€ νν
enum Fruit{
case apple, orange, banana
}
μ΄κ±°ν λ³μμ μμ± λ° κ° λ³κ²½
var myFavoriteFruit: Fruit = .banana
//μ μ½λμ λμΌν νν
var myFavoriteFruit: Fruit = .banana
//κ°μ νμ
μΈ Fruit λ΄λΆ νλͺ©μΌλ‘λ§ highestEduLevel2μ κ° λ³κ²½ κ°λ₯
myFavoriteFruit = .apple
3. μμ κ°
- μ΄κ±°νμ κ° νλͺ©μ κ·Έ μμ²΄λ‘ κ³ μ ν κ°μ΄μ§λ§ νΉμ νμ μΌλ‘ μ§μ λ κ°, μ¦ μμ κ°(Raw Value)λ₯Ό κ°μ§ μ μλ€.
- μΌλΆ νλͺ©λ§ μμ κ°μ μ§μ ν μ μλ€.
- λ¬Έμμ΄ νμ μ΄κ±°νμ μμ κ°μ μ§μ νμ§ μμ νλͺ©μ νλͺ© μ΄λ¦μ΄ μμ κ°, μ¦ rawValue νλ‘νΌν°μ μ μ₯λλ€.
- μ μ νμ μ΄κ±°νμ μμ κ°μ μ§μ νμ§ μμ νλͺ©μ 첫 νλͺ©μ κΈ°μ€μΌλ‘ +0~ 1 μ© μ¦κ°ν κ°μ μμ κ°, μ¦ rawValue νλ‘νΌν°μ μ μ₯λλ€. (μμ κ°μ μ§μ ν΄ μ€ νλͺ©κ³Ό κ·Έλ μ§ μμ νλͺ©μ κ΅μ°¨ λ°°μΉνλ©΄ μ€λ₯ λ°μ)
- rawValue νλ‘νΌν°λ₯Ό ν΅ν΄ μ¬μ©
μ΄κ±°νμ μμ κ° μ§μ κ³Ό μ¬μ©
enum WeekDays: Character {
case mon = "μ", tue = "ν", wed = "μ", thu = "λͺ©", fri = "κΈ"
}
let departureDays: WeekDays = .wed
print("μΆλ° μΌμ? \(departureDays.rawValue)μμΌ") //μΆλ° μΌμ? μμμΌ
μ΄κ±°νμ μμ κ° μΌλΆ μ§μ λ° μλ μ²λ¦¬
//λ¬Έμμ΄ νμ
enum Fruit: String {
case apple = "μ¬κ³Ό"
case orange = "μ€λ μ§"
case banana
}
let myFavoriteFruit: Fruit = .banana
print("λ΄κ° κ°μ₯ μ’μνλ κ³ΌμΌμ \(myFavoriteFruit)μ΄λ€.") //λ΄κ° κ°μ₯ μ’μνλ κ³ΌμΌμ bananaμ΄λ€.
//μ μ νμ
enum Number: Int{
case ten = 10
case zero //11
case one //12
case two//13
//μμκ°μ κ°μ§μ§ μλ νλͺ©μ 첫 νλͺ©μΈ 10μ μμμΌλ‘ 1μ© λμ΄λ κ°μ μμκ°μ κ°κ² λλ€.
//μμκ°μ΄ μλ κ²λΌλ¦¬ λμ΄λμ΄μΌ νλ€. λ€μμ¬ μμΌλ©΄ μ€λ₯
}
//λ³μμ μ΄κ±°νμ μ§μ ν΄ μ£Όμ§ μμλ λλ€.
print(Number1.Zero.rawValue, Number1.one.rawValue, Number1.two.rawValue, Number1.ten.rawValue)
//11 12 13 10
- μμ κ°μ κ°λ μ΄κ±°νμΌ λ, μ΄κ±°νμ μμ κ° μ 보λ₯Ό μλ€λ©΄ μμ κ°μ ν΅ν΄ μ΄κ±°ν λ³μ λλ μμλ₯Ό μμ±ν΄μ€ μλ μλ€.
- λ§μ½ μ¬λ°λ₯΄μ§ μμ, μλ μμ κ°μ ν΅ν΄ μμ±νλ €κ³ νλ€λ©΄ nilμ λ°ννλ€.
let apple = Fruite(rawValue: "μ¬κ³Ό") //apple
let banana = Fruite(rawValue: "λ°λλ") //nil
4. μ°κ΄ κ°
- μ΄κ±°ν λ΄μ νλͺ©(case)μ΄ μμ κ³Ό μ°κ΄λ κ°μ κ°μ§ μ μλ€.
- μ°κ΄ κ°μ κ° νλͺ© μμ μκ΄νΈλ‘ λ¬Άμ΄ ννν μ μλ€.
- μΌλΆ νλͺ©λ§ μ°κ΄ κ°μ κ°μ Έλ λλ€.
μ°κ΄ κ°μ κ°λ μ΄κ±°ν
enum Restaurant {
case steak(done: String)
case pasta(tast: String)
}
let john: Restaurant = .steak(done: well)
- νλͺ©μ μ°κ΄ κ°μ νΉμ νλͺ©μΌλ‘ νμ μ§μΌλ €λ©΄ μ΄κ±°νμΌλ‘ λ³κ²½νλ©΄ λλ€.
enum SteakDone {
case well, medium, rare
}
enum PastaTaste {
case tomato, cream
}
enum Restaurant {
case steak(done: SteakDone)
case pasta(taste: PastaTaste)
}
let rachel: Restaurant = .steak(done: SteakDone.rare)
5. νλͺ© μν
- λͺ¨λ νλͺ©(case)μ 컬λ μ μ μμ±νμ¬ μ΄κ±°νμ νλͺ© μνλ₯Ό ν μ μλ€.
- μ΄κ±°νμ ν¬ν¨λ λͺ¨λ μΌμ΄μ€λ₯Ό μμμΌ ν λ μ¬μ©νλ€.
- μ΄κ±°ν μ μΈ μ CaseIterable νλ‘ν μ½μ μ±ννμ¬ allCases νλ‘νΌν°λ₯Ό ν΅ν΄ λͺ¨λ μΌμ΄μ€μ 컬λ μ μ μμ±νλ€.
CaseIterable νλ‘ν μ½
A type that provides a collection of all of its values.
Types that conform to the CaseIterableprotocol are typically enumerations without associated values. When using a CaseIterable type, you can access a collection of all of the type’s cases by using the type’s allCases property.
CaseIterable νλ‘ν μ½μ λͺ¨λ κ°μ 컬λ μ μ μ 곡νλ€.μ΄ νλ‘ν μ½μ μ€μνλ νμ μ μΌλ°μ μΌλ‘ μ°κ΄ κ°μ΄ μλ μ΄κ±°νμ΄λ€. CaseIterable νλ‘ν μ½μ μ¬μ©νλ©΄, allCases νλ‘νΌν°λ₯Ό μ¬μ©νμ¬ λͺ¨λ νλͺ©(case)μ 컬λ μ μ μ κ·Όν μ μλ€.
enum Fruite: CaseIterable {
case apple, orange, banana
}
let allCases [Fruite] = Fruite.allCases //λͺ¨λ μΌμ΄μ€μ 컬λ μ
μμ±
print(allCases) //Fruite.apple, Fruite.orange, Fruite.banana
//νμ
νμΈ
print(type(of:allCases)) //Array[Fruite]
μμκ°μ κ°λ μ΄κ±°νμ νλͺ© μν
enum Fruite: String, CaseIterable {
case apple = "μ¬κ³Ό"
case orange = "μ€λ μ§"
case banana = "λ°λλ"
}
let allCases: [Fruite] = Fruite.allCases
print(allCases) //Fruite.apple, Fruite.orange, Fruite.banana
- νλ«νΌ λ³λ‘ μ¬μ© 쑰건μ μΆκ°νλ κ²½μ°, μ΄κ±°νμ νλͺ©(case)μ΄ μ°κ΄ κ°μ κ°λ κ²½μ° μ§μ allCases νλ‘νΌν°λ₯Ό ꡬνν΄ μ£Όμ΄μΌ νλ€. -> μ΄ν κ³΅λΆ μμ
6. μν μ΄κ±°ν
- μν μ΄κ±°νμ μ΄κ±°ν νλͺ©μ μ°κ΄ κ°μ΄ μ΄κ±°ν μμ μ κ°μ΄κ³ μ ν λ μ¬μ©
- indirect ν€μλ μ¬μ©
- νΉμ νλͺ©μλ§ νμ μ μ© → case μμ indirect ν€μλ μμ±
- μ΄κ±°ν μ 체μ μ μ© → enum ν€μλ μμ indirect ν€μλ μμ±
μν μ΄κ±°ν νλͺ© λͺ μ
enum ArithmeticExpression1 {
case number(Int)
indirect case addition(ArithmeticExpression1, ArithmeticExpression1)
indirect case multiplication(ArithmeticExpression1, ArithmeticExpression1)
}
6. λΉκ΅ κ°λ₯ν μ΄κ±°ν
- Comparable νλ‘ν μ½μ μ€μνλ μ°κ΄ κ°λ§ κ°κ±°λ μ°κ΄ κ°μ΄ μλ μ΄κ±°νμ Comparable νλ‘ν μ½μ μ±ννλ©΄ κ° μΌμ΄μ€λ₯Ό λΉκ΅ν μ μλ€.
- νλͺ©(case)μ μμ μμΉν μλ‘ μμ κ°μ κ°μ§λ€.
Comparable νλ‘ν μ½
A type that can be compared using the relational operators <, <=, >=, and >.
The Comparable protocol is used for types that have an inherent order, such as numbers and strings. Many types in the standard library already conform to the Comparable protocol. Add Comparable conformance to your own custom types when you want to be able to compare instances using relational operators or use standard library methods that are designed for Comparable types.
Comparable νλ‘ν μ½μ κ΄κ³ μ°μ°μ <, <= >=, > λ₯Ό μ¬μ©νμ¬ λΉκ΅ν μ μλ νμ μ μλ―Ένλ€.
number, λ¬Έμμ΄κ°μ΄ κ³ μ μ μμλ₯Ό κ°μ§λ νμ μ Comparable νλ‘ν μ½μ μ¬μ©νλ€. νμ€ λΌμ΄λΈλ¬λ¦¬μ λ§μ νμ λ€μ μ΄λ―Έ Comparable νλ‘ν μ½μ μ€μνλ€. κ΄κ³ μ°μ°μλ₯Ό μ¬μ©νμ¬ μΈμ€ν΄μ€λ₯Ό λΉκ΅νκ±°λ Comparable νμ μΌλ‘ λμμΈλ νμ€ λΌμ΄λΈλ¬λ¦¬ λ©μλλ₯Ό μ¬μ©νκΈΈ μν λ μ¬μ©μ μ μ νμ μ Comparableμ μ±ννλ€.
enum Numbers: Comparable {
case one
case two
case tree
}
let one: Numbers = .one
let two: Numbers = .two
if one < two {
print("\(two)")
}
else {
print("\(one)")
}
κ³΅λΆ μλ£
μΌκ³° - μ€μννΈ νλ‘κ·Έλλ° 5ν (SWIFT5)
docs.swift.org