Learn Courses My Dashboard

Unable to see the preview

I receive an error “Missing argument for parameter ‘breakfasts’ in call”.
Does anyone know where am I do wrong?
Thank you.


struct BreakfastView: View {
    
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    var breakfasts: Breakfast

    var body: some View {
        
        
            List() {
                                          
                            
                            HStack(spacing: 20.0) {
                                Image(breakfasts.image)
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 40, height: 40, alignment: .center)
                                    .clipped()
                                    .cornerRadius(5)
                                Text(breakfasts.name)
                            }
                            
                    
                    
                }
                .navigationBarTitle("All Recipes",displayMode: .inline)
                .navigationBarBackButtonHidden(true)
                            .navigationBarItems(leading: Button(action : {
                                self.mode.wrappedValue.dismiss()
                            }){
                                Image(systemName: "arrow.left")
                                    .foregroundColor(.black)
                        })
    }
    }


struct BreakfastView_Previews: PreviewProvider {
    static var previews: some View {
        BreakfastView()
    }
}


struct Breakfast: Identifiable {
    
    var id = UUID().uuidString
    var name:String
    var image:String
    var description:String
    var servings:String
    var ingredients:[String]
    var directions:[String]
    
}

var breakfasts = [
    
    Breakfast(name: "Beef Chow Fun",image: "beef chow fun", description: "5 Miles Away",  servings: "2",  ingredients:[
        "500g flat rice noodles",
        "150g beef",
        "1/2 onion",
        "120g bean sprouts",
        "2 tablespoons of light soy sauce",
        "1 tablespoon dark soy sauce",
        "1/2 teaspoon salt",
        "1 teaspoon sugar",
        "Marinades: 2 1/2 teaspoons light soy sauce, 1/4 teaspoon dark soy sauce, 1 teaspoon corn flour, 3/4 teaspoon sugar, 2 teaspoons water, 1/2 teaspoon sesame oil"
    ], directions: ["Wash the beef first and drain the water. Cut into thin slices horizontally, marinate in the marinade for about 15 minutes, set aside.",
                    "Heat the oil in a wok, pan-fried the beef on both sides over medium-high heat until it is eighty mature, and then set aside.",
                    "Heat the wok again, add onions and fry until soft. Add the bean sprouts and stir-fry until cooked, add the flat rice noodles into wok, stir-fry all the ingredients well. Add the seasonings and stir well. Finally, pour the beef and mix well. Serve."] )
    
]

You declared the variable breakfasts of type Breakfast

You need to add this in the initializer of your preview (which is what the error is saying). You are missing this parameter, haven’t defined it yet.

The preview will look something like this
BreakfastView(breakfast: Breakfast())

You will need to add the properties for the Breakfast variable.

When I put BreakfastView(breakfasts: Breakfast()) under the Previews, it give me error “Missing arguments for parameters ‘name’, ‘image’, ‘description’, ‘servings’, ‘ingredients’, ‘directions’ in call”, “Insert ‘name: <#String#>, image: <#String#>, description: <#String#>, servings: <#String#>, ingredients: <#[String]#>, directions: <#[String]#>’”

‘’‘struct BreakfastView_Previews: PreviewProvider {
static var previews: some View {
BreakfastView(breakfasts: Breakfast())
}
}
‘’’

Yes like I noted, you must fill in values for all these properties!

The gray areas that come up are part of Xcode autocomplete saying that you need to type something here

It looks like you maybe have a global var called breakfasts that contains an array of Breakfast instances (possibly for testing purposes?):

var breakfasts = [
    
    Breakfast(name: "Beef Chow Fun",image: "beef chow fun", description: "5 Miles Away",  servings: "2",  ingredients:[
        "500g flat rice noodles",
        "150g beef",
        "1/2 onion",
        "120g bean sprouts",
        "2 tablespoons of light soy sauce",
        "1 tablespoon dark soy sauce",
        "1/2 teaspoon salt",
        "1 teaspoon sugar",
        "Marinades: 2 1/2 teaspoons light soy sauce, 1/4 teaspoon dark soy sauce, 1 teaspoon corn flour, 3/4 teaspoon sugar, 2 teaspoons water, 1/2 teaspoon sesame oil"
    ], directions: ["Wash the beef first and drain the water. Cut into thin slices horizontally, marinate in the marinade for about 15 minutes, set aside.",
                    "Heat the oil in a wok, pan-fried the beef on both sides over medium-high heat until it is eighty mature, and then set aside.",
                    "Heat the wok again, add onions and fry until soft. Add the bean sprouts and stir-fry until cooked, add the flat rice noodles into wok, stir-fry all the ingredients well. Add the seasonings and stir well. Finally, pour the beef and mix well. Serve."] )
    
]

In which case you can just do BreakfastView(breakfasts: breakfasts[0]) in your preview without having to create a new Breakfast instance.

It works roosterboy !! Thanks again.

Also thank you for the support as well – mikaelacaron