Thursday, October 29, 2026
HomeiOS DevelopmentIOS SWIFT - I can print information from API however i am...

IOS SWIFT – I can print information from API however i am unable to set values and UI with this information

[ad_1]

I despatched information to my ListView, and might print it in console. The issue is that i am unable to replace any values with this information, so cannot additionally replace UI with it.
My app ought to take a metropolis identify which String – put it to fetchWeather() func, obtain information and do all stuff, and after this begin getWeather() func in in ListView and replace values (metropolis), which i exploit in UI.

Code:

WeatherMenager:

 import Basis
    
    struct WeatherManager {
        
    
        let weatherURL = "https://api.openweathermap.org/information/2.5/climate?appid=1341b62508512a4fe92357efadccf50a&models=metric"
        
        func fetchWeather(cityName: String) {
            let urlString = "(weatherURL)&q=(cityName)"
            performRequest(with: urlString)
            return
        }
        
        
        func performRequest(with urlString: String) {
            if let url = URL(string: urlString) {
                let session = URLSession(configuration: .default)
                let job = session.dataTask(with: url) { (information, response, error) in
                    if error != nil {
                        
                        return
                    }
                    if let safeData = information {
                        if let climate = self.parseJSON(safeData) {
                            
                            let listVC = ListView()
                            
                            DispatchQueue.most important.async {
                            
                                listVC.getWeather(climate: climate)
                            }
                        }
                    }
                }
                job.resume()
            }
        }
        
        func parseJSON(_ weatherData: Knowledge) -> WeatherModel? {
            let decoder = JSONDecoder()
            do {
                let decodedData = attempt decoder.decode(WeatherData.self, from: weatherData)
                let id = decodedData.climate[0].id
                let temp = decodedData.most important.temp
                let identify = decodedData.identify
                
                let climate = WeatherModel(conditionId: id, cityName: identify, temperature: temp)
                return climate
                
            } catch {
                return nil
            }
        }
    }

WeatherModel:

import Basis

struct WeatherModel {
    var conditionId: Int
    var cityName: String
    var temperature: Double
    
    var temperatureString: String {
        return String(format: "%.1f", temperature)
    }
    
    var conditionName: String {
        change conditionId {
        case 200...232:
            return "cloud.bolt"
        case 300...321:
            return "cloud.drizzle"
        case 500...531:
            return "cloud.rain"
        case 600...622:
            return "cloud.snow"
        case 701...781:
            return "cloud.fog"
        case 800:
            return "solar.max"
        case 801...804:
            return "cloud.bolt"
        default:
            return "cloud"
        }
    }
    
}

WeatherData:

import Basis

struct WeatherData: Codable {
    let identify: String
    let most important: Major
    let climate: [Weather]
}

struct Major: Codable {
    let temp: Double
}

struct Climate: Codable {
    let description: String
    let id: Int
}

ListView: (place the place i wish to show information)

import SwiftUI

struct ListView: View{
    

    @State var weatherMenager = WeatherManager()
    @State var metropolis : String = ""
    
    @State var textFieldText : String = ""
    
    
    func getWeather(climate : WeatherModel){ // 
        
        print(climate.cityName) //works - print information in console
        print(climate.temperatureString)// works - print information in console
        
        self.metropolis = climate.cityName
        
        print(metropolis) // does not print information similar to it is empty string
        
    }
.... extra code which dont't actually metter - simply swiftui code

I do not know what’s flawed, thanks.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments