Skip to content

Instantly share code, notes, and snippets.

@zoejessica
Created August 18, 2019 23:39
Show Gist options
  • Save zoejessica/02f7d88471364c95b4f105771a99d8b9 to your computer and use it in GitHub Desktop.
Save zoejessica/02f7d88471364c95b4f105771a99d8b9 to your computer and use it in GitHub Desktop.
Showing a selection in a SwiftUI List from an observable object (not working atm!)
class Context: ObservableObject {
init(selectedIngredients: Set<Ingredient>, ingredients: [Ingredient]) {
self.selectedIngredients = selectedIngredients
self.ingredients = ingredients
}
@Published var selectedIngredients: Set<Ingredient>
let ingredients: [Ingredient]
}
struct Chooser : View {
@ObservedObject var context: Context
var body: some View {
List(context.ingredients, id: \.self, selection: $context.selectedIngredients) { ingredient in
Text(ingredient.name)
}
.environment(\.editMode, .constant(.active))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment