Can Someone help, My preview isn't working!


my Xcode crashed many times and it showed a catalog, but I don’t know what to do!
Code:

//
//  personDetail.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import SwiftUI

struct personDetail: View {
    @EnvironmentObject var model: toggleViewModel
    var body: some View {
        NavigationView{
            ScrollView {
                ForEach(model.details){ p in
                    VStack(){
                        Text("Name: \(p.name)")
                        Text("Adress: \(p.address)")
                        Text("Company: \(p.company)")
                        Text("Experience: \(String(p.yearOfExperience))")
                        Divider()
                    }
                }

            }
        }
    }
}

struct personDetail_Previews: PreviewProvider {
    static var previews: some View {
        personDetail()
    }
}

Add .enviromentObject(toggleViewModel()) to your Preview code like this:

struct personDetail_Previews: PreviewProvider {
    static var previews: some View {
        personDetail()
            .environmentObject(toggleViewModel())
    }
}

Still not fixed!


Should that be .environmentObject rather than .enviromentObject ?

(You’re missing an “n” in “environment”.)

2 Likes

@Sam_Grover

As advised by JRudderham above.

Typo’s will give you grief all the time. My apologies. Copy the following Preview code and paste that in place of what you have.

struct personDetail_Previews: PreviewProvider {
    static var previews: some View {
        personDetail()
            .environmentObject(toggleViewModel())
    }
}
1 Like

Also you should begin all structs and classes with capital letters, not lowercase letters.

As following Swift’s style guidelines

1 Like

Sir, is there any problem in The for Each loop I wrote for this code, as it is not showing any result, My for each loop never shows the required results, I have also revisited the lecture many times, but don’t know how I can practice more?

@Sam_Grover

Hi Japinderdeep,

Can you post all your code files in a reply so that we have all the necessary swift files that make up the project. This will make it easier to diagnose what the issue may be.

1 Like

data(Json File):

[
{
    "name":"Inigo Montoya",
    "address":"555 Youkilledmyfather Street",
    "company":"CodeWithChris",
    "yearOfExperience":3
},
{
    "name":"Edna Mode",
    "address":"123 Nocape Lane",
    "company":"CodeWithChris",
    "yearOfExperience":6
},{
    "name":"Japinderdeep Singh",
    "address":"99 Youtalkingtome Road",
    "company":"Apple",
    "yearOfExperience":4
},{
    "name":"Walter Sobchak",
    "address":"8 Dude Place",
    "company":"CodeWithChris",
    "yearOfExperience":5
},{
    "name":"Julius Winnfield",
    "address":"25 Ezekiel Ave",
    "company":"CodeWithChris",
    "yearOfExperience":7
}
]

ToggleViewModel:

//
//  Toggle ViewModel.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import Foundation
class toggleViewModel: ObservableObject{
    @Published var details = [Person]()
    init(){
        self.details = DataService.getLocalData()
    }
}

ToggleModel:

//
//  Toggle Model.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import Foundation
class Person: Decodable, Identifiable{
    var id: UUID?
    var name: String
    var address: String
    var company: String
    var yearOfExperience: Int
}

DataService:

//
//  DataService.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import Foundation
class DataService{
    static func getLocalData() -> [Person]{
        let pathstring = Bundle.main.path(forResource: "Data", ofType: "json")
        guard pathstring != nil else{
            return [Person]()
        }
        let url = URL(fileURLWithPath: pathstring!)
        do {
            // Create a data object
            let data = try Data(contentsOf: url)
            
            // Decode the data with a JSON decoder
            let decoder = JSONDecoder()
            
            do {
                
                let toggleData = try decoder.decode([Person].self, from: data)
                for r in toggleData {
                    r.id = UUID()
                    }
            return toggleData
            }
            catch {
                // error with parsing json
                print(error)
            }
        }
        catch {
            // error with getting data
            print(error)
        }
        
        return [Person]()
    }
}

ToggleTabView:

//
//  ContentView.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import SwiftUI

struct toggleTabView: View {
    var body: some View {
        TabView{
            personDetail().tabItem {
                VStack{
                    Image(systemName: "person.2")
                }
            }
            toggleDetail().tabItem {
                VStack{
                    Image(systemName: "gearshape")
                }
            }
        }.environmentObject(toggleViewModel())
    }
}

struct toggleTabView_Previews: PreviewProvider {
    static var previews: some View {
        toggleTabView()
    }
}

PersonDetails:

//
//  personDetail.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import SwiftUI

struct personDetail: View {
    @EnvironmentObject var model: toggleViewModel
    var body: some View {
        NavigationView{
            ScrollView {
                ForEach(model.details){ p in
                    VStack(){
                        Text("Name: \(p.name)")
                        Text("Adress: \(p.address)")
                        Text("Company: \(p.company)")
                        Text("Experience: \(String(p.yearOfExperience))")
                        Divider()
                    }
                }

            }.navigationTitle("About People")
        }
    }
}

struct personDetail_Previews: PreviewProvider {
    static var previews: some View {
        personDetail().environmentObject(toggleViewModel())
    }
}

ToggleDetails:

//
//  toggleDetail.swift
//  Toogle App
//
//  Created by Sam Grover on 12/06/22.
//

import SwiftUI

struct toggleDetail: View {
    @EnvironmentObject var maodel : toggleViewModel
    var body: some View {
        NavigationView{
            VStack(alignment: .leading){
                //Toggle("Show name: ", isOn: $model.name)
                // I don't know how to write a toggle
            }.navigationTitle("Display prefrences")
        }
    }
}

struct toggleDetail_Previews: PreviewProvider {
    static var previews: some View {
        toggleDetail().environmentObject(toggleViewModel())
    }
}

@Sam_Grover

The only thing that could have been the reason that you were not seeing any details in your PersonDetail View is that the json data file naming convention may be the issue.

If the file name in the App bundle is data.json and in your DataService getLocalData() function you have

let pathstring = Bundle.main.path(forResource: "Data", ofType: "json")

Then you should change either the filename in the Bundle to match or change the file prefix in the above code to data. ie:

let pathstring = Bundle.main.path(forResource: "data", ofType: "json")

The Preview works for me.