Learn Courses My Dashboard

iOS Foundation(SwiftUI) Module 2- Lesson 8 challenge

Despite extensive brainstorming, I am unable to come up with a viable alternative to the solution provided.

Hey thanks for posting!

This is what I did:

     Adds a pineapple to each of our pizzas. Changes the first property to this.
    func addPineapple() {
        // Initialize an empty list of pizzas
        var updatedPizzas = [Pizza]()
        // Loop through our old list of pizzas to find their current toppings
        for pizza in pizzas {
            // Set a pizza we can change equal to the current pizza in the list
            var newPizza = pizza
            // Set this pizza's first topping to pineapple
            newPizza.topping1 = "Pineapple"
            // Add the updated pizza to our new list
        // Set the current pizzas to our new list, because we cannot directly change the current pizzas from here
        // Instead we need to set it to an entirely new list
        Pizza = updatedPizzas

Also, I’d suggest not naming variables with capital letters, and giving the variable name some indication that it is a list. So perhaps renaming your Pizza to pizzaList, or just plural pizzas.

Here’s another pro tip, when posting code, use the tilde symbol, the weird squiggly looking symbol in the top-left of the keyboard ~. Then encapsulating your code within two tildes, so <code>.

1 Like

Your efforts are much appreciated. Can you please share whole ViewModel class code?

You’re welcome, and absolutely! I’ll do you one better than that. See here for the whole codebase: GitHub - agholson/Pizza-App


P.S. spero che ti piace il mio umorismo