Enumeracja to zbiór wartości zgrupowanych w typie zdefiniowanym przez użytkownika. W Swift enumerację tworzy się za pomocą słowa kluczowego: enum:
enumTimerState {
case start, pause, stop
}
Przy pomocy case określasz jakie wartości będzie mieć Twoja enumeracja.
Przykład:
enumTimerState {
case start, pause, stop
}
var timerState: TimerState= .start
print("timer state: \(timerState)") // start
Zauważ, że opcja start ma wartość... start. Jest to domyślne zachowanie.
Możesz jednak nadać opcji inną wartość niż ta domyślna. To tzw.: surowe wartości (ang.: raw values)
Surowe wartości
enumClothesSize: String {
caseS="Small"caseM="Medium"caseL="Large"
}
var size: ClothesSize= .Mprint(size) // Mprint(size.rawValue) // Medium
Dla enumeracji ClothesSize zostały zdefiniowane trzy opcje. Do każdej opcji przypisane są wartości: Small, Medium, Large. To są właśnie wartości surowe.
Żeby się do nich dostać, użyj właściwości rawValue.
Opcjom enumeracji możesz nadać typ:
enumClothesSize: Int {
caseS, M, L
}
Jaką surową wartość będzie mieć opcja S? Będzie to 0, a kolejne 1 i 2.
Możesz też napisać tak:
enumClothesSize: Int {
caseS=1, M, L
}
print(ClothesSize.S.rawValue) // 1
Swift "domyśli" się, że kolejne wartości będą to 2 i 3.
Iteracja po enum
Możesz także iterować po wszystkich opcjach enumeracji. Żeby to zrobić, musisz rozszerzyć enumerację za pomocą protokołu: CaseIterable:
enumTimerState: CaseIterable {
case start, pause, stop
}
for current inTimerState.allCases {
print(current)
}
// start// pause// stop
Żeby iterować po enumeracji, użyj właściwości allCases.
Możesz także iterować po surowych wartościach:
enumClothesSize: String, CaseIterable {
caseS="Small"caseM="Medium"caseL="Large"
}
for current inClothesSize.allCases {
print(current.rawValue)
}
// Small// Medium// Large
Switch i enum
Do obsługi opcji możesz użyć instrukcji switch:
enumTimerState {
case start, pause, stop
}
var timerState: TimerState= .pause
switch timerState {
case .start:
print("Start timer")
case .pause:
print("Pause timer")
case .stop:
print("Stop timer")
}
// Pause timer
enumTimerState {
case start, pause, stop
var label: String {
switchself {
case .start:
return"Timer s running"case .pause:
return"Timer is paused"case .stop:
return"Timer is stopped"
}
}
}
var timerState: TimerState= .start
print(timerState.label) // Timer is running
Za pomocą słowa kluczowego self pobierasz wartość, która przechowuje zmienna przy wywołaniu label.
Metody w enum
Swift umożliwia także używanie metod w enum:
enumTimerState {
case start, pause, stop
funcgetLabel() -> String {
switchself {
case .start:
return"Timer s running"case .pause:
return"Timer is paused"case .stop:
return"Timer is stopped"
}
}
}
var timerState: TimerState= .stop
print(timerState.getLabel()) // Timer is stopped
Powiązane wartości
Enumeracja pozwala dołączyć do każdej opcji dodatkowe informacje. Nazywa się to: powiązane wartości (ang.: associated values).
Składnia wygląda następująco:
case someOption(Int)
Po podaniu nazwy opcji w nawiasach określasz dodatkową informację. Możesz dodać wiele opcji:
case someOption(Int, Int, String)
Dla jednego parametru możesz określić jakiś typ danych, a dla następnego użyć innego typu. Dodatkowo opcje mogą mieć różną liczbę parametrów:
case someOption(Int)
case otherOption(String, Double)
Przykład:
enumTimerState {
case start(String)
case pause(String)
case stop(String)
}
var timerState: TimerState= .start("Timer is starting")
switch timerState {
case .start(let label):
print("Label for start: \(label)")
case .pause(let label):
print("Label for pause: \(label)")
case .stop(let label):
print("Label for stop: \(label)")
}
Możesz także nadać etykiety powiązanym wartościom:
case someOption(value: Int)
Kolejny przykład:
enumClothesSize {
caseS(height: Int)
caseM(height: Int)
caseL(height: Int)
}
var size: ClothesSize= .S(height: 165)
switch size {
case .S(let height):
print("Height for S:", height)
case .M(let height):
print("Height for M:", height)
case .L(let height):
print("Height for L:", height)
}
Nie możesz używać powiązanych wartości wraz z surowymi wartościami, w tym samym czasie.
Za dodanie zadania do listy odpowiedzialna jest w tym teście funkcja addTask. Wywołana jest tam funkcja keyboard z biblioteki user event, która w tym przypadku symuluje wciśnięcie przycisku Enter na klawiaturze.