UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Generic parameter 'SelectionValue' could not be inferred

Forums > SwiftUI

Hi! What is wrong with this code and why does this work for a Picker but not a 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 // Generic parameter 'SelectionValue' could not be inferred
                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
    }
}

3      

Your syntax for List is incorrect. Try this:

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

or this is how I would write it:

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

3      

While this compiles, it does not allow to bind selectedFooBar.

3      

Sorry, I must have misunderstood. What are you trying to do with the list? You can't select an item in a List.

You could add a button in each cell in the list...

List{
   ForEach(FooBar.allCases) { item in
      HStack{
           Text(self.string(from: item))
             .foregroundColor(item == self.selectedFooBar ? Color.green : Color(UIColor.label))
           Spacer()

           Button(action: {
               self.selectedFooBar = item
            }){
                  Text("Push")
                }
       }
    }
}

3      

I did go with the Buttons solution but wasn't sure if that was the proper solution. Thanks!

3      

I posted the same question a couple days ago.

If you can not select a row in a list, then why have the "selection" available in the syntax? Is it for setting the item and not for determining the selection?

If so, then how would I impliment the ability to select a row and add a child to it? Adding a button to every item in a list seems kind of clunky.

TIA

3      

I have similar code in a mac app which allows list item to be selected. Mine uses a ForEach though :

 List(selection: $selectedFontType, content: {
                    ForEach(fontTypes, id:\.self) {
                        Text($0)
                    }
                })

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.