1. Swift κΈ°λ³Έ λ°μ΄ν° νμ
1 - 1. κΈ°λ³Έ λ°μ΄ν° νμ (Bool, Int, UInt, Float, Double, Character, String)
1 - 2. Any, AnyObject, nil, Typealias
>>https://lxxyeon.tistory.com/23
2. 컬λ μ νμ (Array, Dictionary, Set)
>> https://lxxyeon.tistory.com/140
3. μ΄κ±°ν νμ (enum)
π μ΄κ±°ν(Enum)
- μ°κ΄λ νλͺ©λ€μ λ¬Άμ΄μ ννν μ μλ νμ
- Swift λ§μ ν° κ°μ !!! >> Cλ Objective-Cκ° Integerκ°λ€λ‘ μ΄κ±°νμ ꡬμ±ν κ²μ λ°ν΄ Swiftμμλ caseκ°μ΄ string, character, integer, floting κ°λ€μ μ¬μ© κ°λ₯
- κ° caseλ κ·Έ μμ²΄κ° κ³ μ μ κ°
- κ° caseλ μλ¬Έμ μΉ΄λ©μΌμ΄μ€λ‘ μ μ
π‘ μ΄κ±°ν λ¬Έλ²
enum <μ΄λ¦> {
case <μ΄λ¦1>
case <μ΄λ¦2>
case <μ΄λ¦3>, <μ΄λ¦4>, <μ΄λ¦5>
...
}
//ex)
enum Weekday {
case mon
case tue
case wed
case thu, fri, sat, sun
}
π‘ μ΄κ±°ν κ° μ κ·Ό
var day: Weekday = Weekday.mon
day //mon
day = .tue // νμ
μ΄ λͺ
ννλ€λ©΄ μΆμ½ν΄μ μ¬μ© κ°λ₯
print(day) // tue
π‘ Switchλ¬Έμμ μ¬μ©
switch day {//μ΄κ±°ν
case .mon, .tue, .wed, .thu:
// case νλλΌλ μλ κ²½μ°λ default ꡬνν΄μΌν¨
// λͺ¨λ case ꡬνμ default μμ±ν νμ μλ€.
print("νμΌμ
λλ€")
case Weekday.fri:
print("λΆκΈ νν°!!")
case .sat, .sun:
print("μ λλ μ£Όλ§!!")
}
π‘ μμκ°
- νΉμ νμ μΌλ‘ μ§μ λ κ°μ κ°μ§ μ μλ€
- C μΈμ΄μ enumμ²λΌ μ μκ°μ κ°μ§ μ μλ€
- rawValueλ‘ μ μ κ° μ κ·Ό κ°λ₯
- caseλ³λ‘ κ°κ° λ€λ₯Έ κ°μ κ°μ ΈμΌ ν¨
enum Fruit: Int {//μ΄κ±°ν μ΄λ¦ μμ μμκ°μ κ°μ§ νμ
μ μΈ
case apple = 0
case grape = 1//1 μ§μλ μλμΌλ‘ 1 ν λΉ
case peach //1μ© μ¦κ° > peach = 2
// case mango = 0//μ€λ₯ λ°μ©
}
print("Fruit.peach.rawValue == \(Fruit.peach)") //peach
print("Fruit.peach.rawValue == \(Fruit.peach.rawValue)") //2
// Fruit.peach.rawValue == 2
//μ μ νμ
λΏλ§ μλλΌ Hashable νλ‘ν μ½μ λ°λ₯΄λ λͺ¨λ νμ
μ΄ μμκ°μ νμ
μΌλ‘ μ§μ λ μ μμ΅λλ€
enum School: String {
case elementary = "μ΄λ±"
case middle = "μ€λ±"
case high = "κ³ λ±"
case university //rawvalue κ° : case μ΄λ¦
}
print("School.middle.rawValue == \(School.middle.rawValue)")
// School.middle.rawValue == μ€λ±
print("School.university.rawValue == \(School.university.rawValue)")
// School.university.rawValue == university //μΌμ΄μ€μ΄λ¦κ°μ Έμ΄
π‘ μμκ°μ ν΅ν μΌμ΄μ€ μ΄κΈ°ν
- rawValueλ₯Ό ν΅ν΄ μ΄κΈ°ν κ°λ₯
- rawValueκ° caseμ ν΄λΉνμ§ μμ μ μμΌλ―λ‘ rawValueλ₯Ό ν΅ν΄ μ΄κΈ°ν ν μΈμ€ν΄μ€λ μ΅μ λ νμ
enum Fruit: Int {
case apple = 0
case grape = 1
case peach
}
//let apple: Fruit = Fruit(rawValue: 0) // μΌλ°νμ
μ ν λΉνλ €κ³ νλ©΄ μ€λ₯λ°μ
let apple: Fruit? = Fruit(rawValue: 2) // μ΅μ
λ νμ
enum Numbers: Int{
case zero
case one=2
}
print("\(Numbers.zero.rawValue)")//0
var one = Numbers(rawValue: 2) //rawValue κ°μ κ°μ§ caseλ?
//print("\(one)")//optional νμ
!
var one2: Numbers? = Numbers(rawValue: 0)
if let orange: Fruit = Fruit(rawValue: 5) {
print("rawValue 5μ ν΄λΉνλ μΌμ΄μ€λ \(orange)μ
λλ€")
} else {
print("rawValue 5μ ν΄λΉνλ μΌμ΄μ€κ° μμ΅λλ€")
} // rawValue 5μ ν΄λΉνλ μΌμ΄μ€κ° μμ΅λλ€
π‘ λ©μλ μΆκ° κ°λ₯
enum Month {
case dec, jan, feb
case mar, apr, may
case jun, jul, aug
case sep, oct, nov
func printMessage() { //λ©μλ μΆκ°
switch self {
case .mar, .apr, .may:
print("λ°μ€ν λ΄~")
case .jun, .jul, .aug:
print("μ¬λ¦ λμμ~")
case .sep, .oct, .nov:
print("κ°μμ λ
μμ κ³μ !")
case .dec, .jan, .feb:
print("μΆμ΄ 겨μΈμ
λλ€")
}
}
}
Month.mar.printMessage()
// λ°μ€ν λ΄~
728x90
λ°μν