Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SwiftUI
//
// ContentView.swift
// ListSampleUsingCaseIterable
//
// Created by 能登 要 on 2021/04/14.
//
import SwiftUI
enum AnimalStatus: String {
case stable
case angry
case hungry
}
struct Animal {
var id: String
var name: String
var status: AnimalStatus
init(name: String, status: AnimalStatus) {
id = name
self.name = name
self.status = status
}
}
enum Animals: String, CaseIterable, CustomStringConvertible {
case dogs
case elephants
var description: String {
return self.rawValue
}
var items: [Animal] {
switch self {
case .dogs:
return [
Animal(name: "Colin", status: .stable),
Animal(name: "Irwin", status: .stable),
]
case .elephants:
return [
Animal(name: "Ahmed", status: .stable),
Animal(name: "Nasser", status: .stable),
]
}
}
}
struct ContentView: View {
var body: some View {
List {
ForEach(Animals.allCases, id: \.self) { animal in
Section(header: Text(String(describing: animal)) ) {
ForEach(animal.items, id: \.name) { item in
Text(String(describing: item.name))
}
}
.textCase(.none)
}
}.listStyle(PlainListStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment