Swift

[ SWIFT ] 데이터 νƒ€μž… κ³ κΈ‰(3) - μ—΄κ±°ν˜•

Forest Yun 2021. 7. 30. 00:18
728x90

 

κ³΅λΆ€ν•œ λ‚΄μš©μ„ μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.

 

 

 

데이터 νƒ€μž… κ³ κΈ‰(3)

  1. μ—΄κ±°ν˜•
  2. κΈ°λ³Έ μ—΄κ±°ν˜•
  3. μ›μ‹œ κ°’
  4. μ—°κ΄€ κ°’
  5. ν•­λͺ© 순회
  6. μˆœν™˜ μ—΄κ±°ν˜•
  7. 비ꡐ κ°€λŠ₯ν•œ μ—΄κ±°ν˜•

 

 

 

 

 

 


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

 

 

728x90