Last active
April 14, 2021 05:43
-
-
Save notoroid/ae6ee69dbab03313df7f0dde010bd1e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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