SwiftUI 使用枚举构建列表
SwiftUI build a list using enums
我正在尝试基于枚举字符串创建 SwiftUI 列表。我遇到了这个错误:
Cannot invoke initializer for type 'List<_, _>' with an argument list of type '([HomeView.Data], @escaping (String) -> HomeMenuRow)'
我不明白如何使用 id 或如何遍历枚举来构建行。
试试这个:
enum Whatever : String, CaseIterable, Identifiable {
var id : String { UUID().uuidString }
case one = "one"
case two = "two"
}
struct ContentView: View {
var body: some View {
VStack {
List (Whatever.allCases) { value in
Text(value.rawValue)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我正在尝试基于枚举字符串创建 SwiftUI 列表。我遇到了这个错误:
Cannot invoke initializer for type 'List<_, _>' with an argument list of type '([HomeView.Data], @escaping (String) -> HomeMenuRow)'
我不明白如何使用 id 或如何遍历枚举来构建行。
试试这个:
enum Whatever : String, CaseIterable, Identifiable {
var id : String { UUID().uuidString }
case one = "one"
case two = "two"
}
struct ContentView: View {
var body: some View {
VStack {
List (Whatever.allCases) { value in
Text(value.rawValue)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}