NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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.

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.