Display -IOS in 90 Days -Day 26 Challenge

Hi,

I could not get the display for this challenge and I’m unsure where I messed up. Any advice? Codes are pasted below.

Thanks in advance!

//

// Pizza.swift

// Day 23 Challenge

//

// Created by Ellie on 2/18/23.

//

**import** Foundation

**struct** Pizza: Identifiable, Decodable {

**var** id:UUID?

**var** name:String

**var** toppings:[String]

**var** image:String

}

//
//  PizzaModel.swift
//  Day 23 Challenge
//
//  Created by Ellie on 2/18/23.
//

import Foundation

class PizzaModel: ObservableObject {
    
    @Published var pizzas = [Pizza]()
    
    init() {
        
        let pathString = Bundle.main.path(forResource: "pizzas", ofType: "json")
        if pathString != nil {
            
            let url = URL(fileURLWithPath: pathString!)
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                
                do {
                    var jsonPizzas = try decoder.decode([Pizza].self, from: data)
                    for index in 0..<jsonPizzas.count {
                        jsonPizzas[index].id = UUID()
                    }
                    self.pizzas = jsonPizzas
                }
                catch {
                    print("Couldn't parse Pizzas")
                }
                
            }
            catch {
                print("Couldn't create Data object")
            }
        }
    }
}

//
//  ContentView.swift
//  Day 23 Challenge
//
//  Created by Ellie on 2/18/23.
//

import SwiftUI

struct ContentView: View {
    @ObservedObject var model = PizzaModel()
    
    var body: some View {
        List(model.pizzas) { pizza in
            
            HStack {
                Image(pizza.image)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                frame(width: 60, height: 60)
                    .cornerRadius(5)
                    .clipped()
                
                VStack(alignment: .leading) {
                    Text(pizza.name)
                        .font(.headline)
                    HStack {
                        Text(pizza.toppings[0])
                        Text(pizza.toppings[1])
                        Text(pizza.toppings[2])
                    }
                    .font(.caption)
                }
            }
            
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
}

//
//  Day_23_ChallengeApp.swift
//  Day 23 Challenge
//
//  Created by Ellie on 2/18/23.
//

import SwiftUI

@main
struct Day_23_ChallengeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

[
    {"name": "Meat Lovers",
        "toppings": ["Pepperoni", "Bacon", "Sausage"]
    },
    {"name: "Hawaiian",
        "toppings": ["Ham", "Bacon", "Pineapple"]
    },
    {"name": "Deluxe",
        "toppings": ["Pepperoni", "Green Peppers", "Mushroom"]
    }
]




@ellieN1235

Hi Ellie,

Rename your images so that the names match the name that you have in your json file. For example, rename the image you have for your meat lovers pizza to Meat Lovers. Do the same for the other images.

In your ContentView where you have Image(pizza.image), change that to Image(pizza.name) and also ensure that you have a . in front of the frame(width: 60, height: 60) since all modifiers must be prefixed with a .

Hi,

I fixed it as you said but I still couldn’t see the display. I’m pasting codes below.

[
    {"name": "Meat Lovers",
        "toppings": ["Pepperoni", "Bacon", "Sausage"]
    },
    {"name: "Hawaiian",
        "toppings": ["Ham", "Bacon", "Pineapple"]
    },
    {"name": "Deluxe",
        "toppings": ["Pepperoni", "Green Peppers", "Mushroom"]
    }
]


//
//  PizzaModel.swift
//  Day 23 Challenge
//
//  Created by Ellie on 2/18/23.
//

import Foundation

class PizzaModel: ObservableObject {
    
    @Published var pizzas = [Pizza]()
    
    init() {
        
        let pathString = Bundle.main.path(forResource: "pizzas", ofType: "json")
        if pathString != nil {
            
            let url = URL(fileURLWithPath: pathString!)
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                
                do {
                    var jsonPizzas = try decoder.decode([Pizza].self, from: data)
                    for index in 0..<jsonPizzas.count {
                        jsonPizzas[index].id = UUID()
                    }
                    self.pizzas = jsonPizzas
                }
                catch {
                    print("Couldn't parse Pizzas")
                }
                
            }
            catch {
                print("Couldn't create Data object")
            }
        }
    }
}


//
//  ContentView.swift
//  Day 23 Challenge
//
//  Created by Ellie on 2/18/23.
//

import SwiftUI

struct ContentView: View {
    @ObservedObject var model = PizzaModel()
    
    var body: some View {
        List(model.pizzas) { pizza in
            
            HStack {
                Image(pizza.name)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 60, height: 60)
                    .cornerRadius(5)
                    .clipped()
                
                VStack(alignment: .leading) {
                    Text(pizza.name)
                        .font(.headline)
                    HStack {
                        Text(pizza.toppings[0])
                        Text(pizza.toppings[1])
                        Text(pizza.toppings[2])
                    }
                    .font(.caption)
                }
            }
            
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
}



//

// Pizza.swift

// Day 23 Challenge

//

// Created by Ellie on 2/18/23.

//

**import** Foundation

**struct** Pizza: Identifiable, Decodable {

**var** id:UUID?

**var** name:String

**var** toppings:[String]

**var** image:String

}