Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Initialize @Binding SwiftUi

I cannot seem to figure out how to initialize a @Binding that is of type Array:

struct RecipeItemDetailView: View {

@Binding var recipeDetails: [StoredRecipeModel]

var body: some View {
    NavigationView {
        VStack {
            Text(recipeDetails[1].name)
        }
    }
}

struct RecipeItemDetailView_Previews: PreviewProvider {
    static var previews: some View {
        RecipeItemDetailView(recipeDetails: <#Binding<[StoredRecipeModel]>#>)
}

In the above, you will see that in the PreviewProvider after "recipeDetails: " it is asking that I initialize recipeDetails. I put the sample code in that is is asking for. I am able to initialize less complex bindings (e.g. .constant(false) ) but that does not work in this case.

Any thoughts?

Hopefully I am using the correct terms here, as I am quite new to programming!

like image 459
nickreps Avatar asked Jul 31 '26 07:07

nickreps


1 Answers

Update: I was able to figure it out. The following code appears to work:

struct RecipeItemDetailView_Previews: PreviewProvider {
static var previews: some View {
    RecipeItemDetailView(recipeDetails: .constant([StoredRecipeModel].init()))
}
like image 68
nickreps Avatar answered Aug 03 '26 01:08

nickreps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!