|
I need some help with a small SwiftUI app - probably with a simple solution - I cant't find! Just started Xcode and Swift a week ago... I had a look at the free available example: Sending and receiving Codable data with URLSession and SwiftUI from november 18th 2021. See link: https://www.hackingwithswift.com/books/ios-swiftui/sending-and-receiving-codable-data-with-urlsession-and-swiftui I start to understand whats going on with SwiftUI, the app runs nicely! Now I try to do one app by myself for work. A App should show 4 string values from a JSON servercall. B This is the JSON file - its rewritten every couple of minutes on the server: [{"tempNow":"26","tempMax":"159","pressureNow":"159","pressurePeak":"159"}] JSON URL: https://www.worknode.com/xcode/serverdata.json C There are 4 values I like to get out of the JSONDecoder as pure string value like: var tempNow, var tempMax, varpressureNow, var pressureMax D Then I just like to give the value into a Text() for displaying in usuall SwiftUI var body: some View {
}: So I dont want to have a List but a simple Text() field. Any idears? Thanks for some help chriskapeller Here the example code with JSON(from itunes server) and the List: import SwiftUI struct Response: Codable { var results: [Result] } struct Result: Codable { var trackId: Int var trackName: String var collectionName: String } struct PostListView: View {
} |
|
Hi, the issue as I see it is that the data is in an array
Here is the struct
and this is the extension on URLSession (only included if you use the
|
|
Hi Nigel, thanks for your Insight on this problem. I can follow your code...and could use it in this caase. I guess I will tinker around a bit more. About the original code: You write that the problem lies in the asynchronous loading and that the empty array [ ] causes further problems. Would it also be a solution to fill the array with some junk data right at the beginning? Then async load the JSON - is this possible or did I not understand the real cause? tx chriskapeller |
|
Hi Chris, When the View appears the
You get
However I went with Progress View as sometime the loading from internet can sometime take a while (or never happen due to user internet connection) so went with a ProgressView to tell user something is happening. |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.