【发布时间】:2026-01-22 17:30:01
【问题描述】:
我可能遗漏了一些东西,但为什么这对 Picker 工作得很好,但对 List 却不行?我不明白为什么它抱怨缺少参数类型。
struct ContentView: View {
enum FooBar: CaseIterable, Identifiable {
public var id : String { UUID().uuidString }
case foo
case bar
case buzz
case bizz
}
@State var selectedFooBar: FooBar = .bar
var body: some View {
VStack {
Picker("Select", selection: $selectedFooBar) {
ForEach(FooBar.allCases) { item in
Text(self.string(from: item)).tag(item)
}
}
List(FooBar.allCases, selection: $selectedFooBar) { item in
Text(self.string(from: item)).tag(item)
}
Text("You selected: \(self.string(from: selectedFooBar))")
}
}
private func string(from item: FooBar) -> String {
var str = ""
switch item {
case .foo:
str = "Foo"
case .bar:
str = "Bar"
case .buzz:
str = "Buzz"
case .bizz:
str = "Bizz"
}
return str
}
}
我试图找到解释和例子,但找不到任何东西。
【问题讨论】:
标签: swiftui swiftui-list