Is SwiftUI Picker not compatible with a Bindable property?

I have a view with @Bindable property. (It's a model)

I can use TextField("Name", text: $chore.name), but not Picker(selection: $chore.choreFrequency, label: EmptyView())

The auto-complete doesn't even show choreFrequency as an option

enum ChoreFrequency: String, CaseIterable, Identifiable, Codable {
	case daily, weekly, monthly, seasonal
	var id: ChoreFrequency { self }
}

@Model
class Chore: Identifiable {
	@Attribute(.unique) var id = UUID()
	var name: String
	var frequency: ChoreFrequency
	var assignedTo: FamilyMember
	var isComplete: Bool
	var dueDate: Date
}

Can I not use the Bindable property in the Picker?

Replies

You should show the complete code. Did you set a tag for each picker case ?

Can't you simply use $chore and pass the property in the Texts of the picker ?:

 Picker(selection: $chore, label: EmptyView())
  • If not done already, see Picker documentation in Xcode for more details.

For example, consider an enumeration of ice cream flavors and a State variable to hold the selected flavor:

enum Flavor: String, CaseIterable, Identifiable {
    case chocolate, vanilla, strawberry
    var id: Self { self }
}

@State private var selectedFlavor: Flavor = .chocolate

You can create a picker to select among the values by providing a label, a binding to the current selection, and a collection of views for the picker’s content. Append a tag to each of these content views using the tag(_:) view modifier so that the type of each selection matches the type of the bound state variable:

List {
    Picker("Flavor", selection: $selectedFlavor) {
        Text("Chocolate").tag(Flavor.chocolate)
        Text("Vanilla").tag(Flavor.vanilla)
        Text("Strawberry").tag(Flavor.strawberry)
    }
}

Sorry, forgot to include the view code:

import SwiftUI
import CommonUI
import SwiftData

struct ChoreView: View {
	@Bindable var chore: Chore
	@Environment(\.modelContext) private var context
	@Query private var familyMembers: [FamilyMember]

    var body: some View {
		 Card {
			 Form {
				 Section("Name") {
					 TextField("Name", text: $chore.name)
				 }

				 Section("Frequency") {
					 Picker(selection: $chore.choreFrequency, label: EmptyView()) {
						 ForEach(ChoreFrequency.allCases) { frequency in
							 Text(frequency.rawValue.localized).tag(frequency.rawValue)
						  }
					 }
				 }
			 }
			 .frame(maxHeight: 400)
			 .padding(-6)
		 }
    }
}

I am an *****. Never mind, I got the property name wrong.