TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Nested Array and JSON - Purely Learning

Forums > SwiftUI

I cannot figure out how to grab info from nested array and use in my list.

MODEL INFO

struct WeatherForcast: Codable {
    let weatherDate: Double
    let sunrise: Double
    let sunset: Double
    let weatherTemp: WeatherTemp
    let weatherInfo: [WeatherTempInfo]

    private enum CodingKeys: String, CodingKey {
        case weatherDate = "dt"
        case sunrise = "sunrise"
        case sunset = "sunset"
        case weatherTemp = "temp"
        case weatherInfo = "weather"
    }
}

struct WeatherTempInfo: Codable {
    let sky: String
    let description: String
    let icon: String

    private enum CodingKeys: String, CodingKey {
        case sky = "main"
        case description = "description"
        case icon = "icon"
    }
}

VIEW MODEL INFO Now the func I placed in the View Model works and displays the data, but I can't figure out how to displays the days info based on parent array index or date value.

class WeatherForcastViewModel: Identifiable {
    let id = UUID()
    let weatherDate: Double
    let sunrise: Double
    let sunset: Double
    let weatherTemp: WeatherTemp
    let weatherTempInfo: [WeatherTempInfo]

    init(weatherForcast: WeatherForcast) {
        self.weatherDate = weatherForcast.weatherDate
        self.sunrise = weatherForcast.sunrise
        self.sunset  = weatherForcast.sunset
        self.weatherTemp = weatherForcast.weatherTemp
        self.weatherTempInfo = weatherForcast.weatherInfo
    }

    func listWeatherTempInfo() {
        weatherTempInfo.forEach { (item) in
            print("Sky Info: \(item.sky)")
        }
    }
}

class WeatherListForcastViewModel: ObservableObject {
    @Published var forcasts = [WeatherForcastViewModel]()
    @Published var weatherTempInfo = [WeatherTempInfo]()
    var weatherSkyInfo = ""
    private var weatherDescriptionInfo = ""
    private var weatherIconInfo = ""
    let jsonService = JSONService()

    func getForcastByCityId(cityId: Int) {
        jsonService.getCityWeatherById(cityId: cityId) { result in
            switch result {
            case .success(let details):
                if let details = details {
                    DispatchQueue.main.async {
                        self.forcasts = details.map(WeatherForcastViewModel.init)
                        self.getWeatherItems()

                    }
                }
            case .failure(let error):
                print(error.localizedDescription)
                DispatchQueue.main.async {
                    print(error.localizedDescription)
                }
            }
        }
    }

    func getWeatherItems() {
        forcasts.forEach { (item) in
            item.weatherTempInfo.forEach { (x) in
                print("Forcast Description: \(x.sky)")
                weatherSkyInfo = x.sky
            }
        }
    }

LAYOUT I have a vertical scrollview, which has a horizontal scrollview and in each hstack I have a vertical scrollview where I want to place the nested array info. I am able to list out seven days of weather and it works without issue. I just can't figure out how to grab the corresponding info in the nested array.

2      

Can You publish the code of json service please

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.