Swiftui 无法推断泛型参数“SelectionValue”

Swiftui 无法推断泛型参数“SelectionValue”,swiftui,swiftui-list,Swiftui,Swiftui List,我可能遗漏了一些东西,但为什么这对于一个挑选者来说很好,但对于一个列表来说却不行呢?我不明白为什么它会抱怨缺少参数类型 struct ContentView: View { enum FooBar: CaseIterable, Identifiable { public var id : String { UUID().uuidString } case foo case bar case buzz case bizz } @State var

我可能遗漏了一些东西,但为什么这对于一个挑选者来说很好,但对于一个列表来说却不行呢?我不明白为什么它会抱怨缺少参数类型

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
}
}

我试图找到解释和示例,但什么也找不到。

所选项目的选择器的FooBar类型正确,但列表的FooBar类型错误。如果您使用Set,那么编译器不会抱怨

@State var selectedFooBar: FooBar = .bar
@State var selectedItems: Set<String>

var body: some View {
    VStack {
        Picker("Select", selection: $selectedFooBar) {
            ForEach(FooBar.allCases) { item in
                Text(self.string1(from: item)).tag(item)
            }
        }

        List(FooBar.allCases, selection: $selectedItems) { item in
            Text(self.string1(from: item))
        }



        Text("You selected: \(self.string1(from: selectedFooBar))")
    } 

请注意,列表需要处于编辑模式才能使用选择。

所选项目的选择器FooBar类型正确,但列表的类型错误。如果您使用Set,那么编译器不会抱怨

@State var selectedFooBar: FooBar = .bar
@State var selectedItems: Set<String>

var body: some View {
    VStack {
        Picker("Select", selection: $selectedFooBar) {
            ForEach(FooBar.allCases) { item in
                Text(self.string1(from: item)).tag(item)
            }
        }

        List(FooBar.allCases, selection: $selectedItems) { item in
            Text(self.string1(from: item))
        }



        Text("You selected: \(self.string1(from: selectedFooBar))")
    } 

请注意,列表需要处于编辑模式才能使用选择。

如果您将列表提取到其自身的正文外变量:

…出现以下错误:无法使用类型为“[ContentView.FooBar]的参数列表调用类型为“List”的初始值设定项,选择:Binding,@escaping ContentView.FooBar->some View”

看起来您试图使用列表上Picker中的相同初始值设定项,但它们完全不同。也许这就是你想要的:

List {
            ForEach(FooBar.allCases) { item in
                Text(self.string(from: item)).tag(item)
            }
        }

如果将列表提取到其自身变量的正文外:

…出现以下错误:无法使用类型为“[ContentView.FooBar]的参数列表调用类型为“List”的初始值设定项,选择:Binding,@escaping ContentView.FooBar->some View”

看起来您试图使用列表上Picker中的相同初始值设定项,但它们完全不同。也许这就是你想要的:

List {
            ForEach(FooBar.allCases) { item in
                Text(self.string(from: item)).tag(item)
            }
        }