Data Flow Day 19 (Learn IOS in 90 Days)

Hi,

I’m on Day 19 of the Learn IOS in 90 Days Program. I’m towards the end of the challenge but there are some errors popping up and I’m not sure how to fix them. Any advice?

I’m attaching my codes below.

Thanks in advance!



In the init() method, where the warning is showing up, change each of the var to a let.
What the warning is saying is that since you are not changing pizza1 or pizza2 or pizza3, there is no need to declare it as a variable because that implies that you place to change it somewhere.

In the case of the Error in the second screen shot, how have you declared addPinapple()?

Can you paste your code from your model.

When you paste code in, paste it as text and then format it as a code block. To do that, select the code you just pasted in and then from the compose post toolbar tap the Pre-formatted text button which looks like this </>

Hi Chris,

Are you asking about my “addPineapple()” in ContentView?
Here are the codes.

import SwiftUI

struct ContentView: View {
    @ObservedObject var model = ViewModel()
    
    var body: some View {
        VStack {
            
            List (model.pizzas) { pizza in
                
                VStack(alignment: .leading) {
                    Text(pizza.name)
                        .font(.headline)
                    
                    HStack {
                        Text(pizza.topping1)
                        Text(pizza.topping2)
                        Text(pizza.topping3)
                    }
                }
            }
            
            Button("Add Pineapple") {
                model.addPineapple()
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
}

@ellieN1235

Hi Ellie,

I’ve been away for a break hence no replies.

Can you post the code from your ViewModel where you have the function addPineapple()?

Hi Chris,

Here are the codes:

//
//  RecipeModel.swift
//  day18mvvmchallenge
//
//  Created by Ellie on 2/15/23.
//

import Foundation

class ViewModel: ObservableObject {
    
    @Published var pizzas = [pizza]()
    
    init() {
        
        let pizza1 = pizza()
        pizza1.name = "Meat Lovers"
        pizza1.topping1 = "Pepperoni"
        pizza1.topping2 = "Bacon"
        pizza1.topping3 =  "Sausage"
        
        pizzas.append(pizza1)
        
        let pizza2 = pizza()
        pizza2.name = "Deluxe"
        pizza2.topping1 = "Pepperoni"
        pizza2.topping2 = "Mushroom"
        pizza2.topping3 = "Green Peppers"
        
        pizzas.append(pizza2)
        
        let pizza3 = pizza()
        pizza3.name = "Hawaiian"
        pizza3.topping1 = "Ham"
        pizza3.topping2 = "Bacon"
        pizza3.topping3 = "Pineapple"
        
        pizzas.append(pizza3)
        
        func addPineapple() {
            for i in 0..<pizzas.count {
                pizzas[i].topping1 = "Pineapple"
            }
        }
        
        
        
    }
    
}

Hi Ellie,

Your function addPineapple() is inside the init() method. It needs to outside the init() method so that it is at the same level as the init() like this:

import Foundation

class ViewModel: ObservableObject {
    
    @Published var pizzas = [pizza]()
    
    init() {
        
        let pizza1 = pizza()
        pizza1.name = "Meat Lovers"
        pizza1.topping1 = "Pepperoni"
        pizza1.topping2 = "Bacon"
        pizza1.topping3 =  "Sausage"
        
        pizzas.append(pizza1)
        
        let pizza2 = pizza()
        pizza2.name = "Deluxe"
        pizza2.topping1 = "Pepperoni"
        pizza2.topping2 = "Mushroom"
        pizza2.topping3 = "Green Peppers"
        
        pizzas.append(pizza2)
        
        let pizza3 = pizza()
        pizza3.name = "Hawaiian"
        pizza3.topping1 = "Ham"
        pizza3.topping2 = "Bacon"
        pizza3.topping3 = "Pineapple"
        
        pizzas.append(pizza3)
    }

    func addPineapple() {
        for i in 0..<pizzas.count {
            pizzas[i].topping1 = "Pineapple"
        }
    }
}