Member-only story
Building Multiplatform Weather App using KMM : Part 4 — Updating Data on iOS UI
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…