0

I am a beginner developer and I am getting this error "Cannot find 'previewWeather' in scope" when I have the "previewWeather" variable in my "Model Data" file?

struct WeatherView: View { var weather: ResponseBody

var body: some View {
    ZStack(alignment: .leading) {
        VStack {
            VStack(alignment: .leading, spacing: 5) {
                Text(weather.name)
                    .bold().font(.title)
                
                Text("Today,\(Date().formatted(.dateTime.month() .day().hour().minute()))")
                    .fontWeight(.light)
                
            }
            .frame(maxWidth: .infinity, alignment: .leading)
            
            Spacer()
            
            VStack {
                HStack {
                    VStack(spacing: 20) {
                        Image(systemName: "sun.max.fill")
                            .font(.system(size: 40))
                        
                        Text(weather.weather[0].main)
                        
                    }
                    .frame(width: 150, alignment: .leading)
                    
                    Spacer()
                    
                    
                    Text(weather.main.feels_like.roundDouble() + "°")
                        .font(.system(size: 100))
                        .fontWeight((.bold))
                        .padding()
                }
                
                Spacer()
                    .frame(height: 80)
                
                AsyncImage(url: URL(string: "https://cdn.pixabay.com/photo/2020/01/24/21/33/city-4791269_960_720.png")) { image in image
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 350)
                } placeholder: {
                    ProgressView()
                }
                
                Spacer()
                
            }
            .frame(maxWidth: .infinity)
            
        }
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
        
        VStack {
            Spacer()
            
            VStack(alignment: .leading, spacing: 20) {
                Text("Weather now")
                    .bold().padding(.bottom)
                
                HStack {
                    WeatherRow(logo: "thermometer", name: "Min temp", value: (weather.main.temp_min.roundDouble() + "°"))
                    Spacer()
                    WeatherRow(logo: "thermometer", name: "Max temp", value: (weather.main.temp_max.roundDouble() + "°"))
                }
                
                HStack {
                    WeatherRow(logo: "wind", name: "Wind speed", value: (weather.wind.speed.roundDouble() + "m/s"))
                    Spacer()
                    WeatherRow(logo: "humidity", name: "Humidity", value: (weather.main.humidity.roundDouble() + "%"))
                }
            }
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding()
            .padding(.bottom, 20)
            .foregroundColor(Color(hue: 0.685, saturation: 0.988, brightness: 0.377))
            .background(.white)
            .cornerRadius(20, corners: [.topLeft, .topRight])
        }
        
        
    }
    .edgesIgnoringSafeArea(.bottom)
    .background(Color(hue: 0.685, saturation: 0.988, brightness: 0.377))
    .preferredColorScheme(.dark)
}

}

struct WeatherView_Previews: PreviewProvider { static var previews: some View { WeatherView(weather: previewWeather) < -Error is hereScreenshot of my code } }

import Foundation

// previewWeather var here -> var previewWeather: ResponseBody = load("weatherData.json").

func load<T: Decodable>(_ filename: String) -> T { let data: Data

guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
    fatalError("Couldn't find \(filename) in main bundle.")
}

do {
    data = try Data(contentsOf: file)
} catch {
    fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}

do {
    let decoder = JSONDecoder()
    return try decoder.decode(T.self, from: data)
} catch {
    fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}

}

1 Answer 1

0

Try this to fix your error:

struct Weather_Previews: PreviewProvider {

static var previewWeather = previewWeather() // <- your "Model Data" 
   
    static var previews: some View {
        WeatherView(weather: previewWeather)
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.