Member-only story

Building Multiplatform Weather App using KMM : Part 4 — Updating Data on iOS UI

Andrea
1 min readDec 14, 2023

Similar with part 3, we will also updating our data on iOS platform.

First step I will change all data into some variable inside our content view.

struct ContentView: View {
@State private var place = ""
@State private var temp = ""
@State private var description = ""

var api = WeatherAPI();
...

For convenient, I will create function which will called API Function and updating the variable when the function is called successfully.

func retrieveData() {
// called our API Function & update variable
api.getWeatherAPIData (successFunction: { it in
place = it.name ?? "";
guard let tempDouble = it.main.temp as? Double
else {
temp = "error"
return
}
if (temp != "error") {
temp = String(format: "%.0f", tempDouble - 273.0)}
guard let weather : Weather = it.weather[0] as? Weather else {
return
}
description = weather.description_ ?? ""
},
failureFunction: { error in
place = error
})
}

On the last step, I will called retrieveData() function when UI appeared

var api = WeatherAPI();

var body: some View {
Color.black
.ignoresSafeArea()
.overlay(
VStack

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Andrea
Andrea

No responses yet

Write a response