|
Hi everyone, first post for me here.. I am The next part (weather) is contained inside an array and I can't understand how to organise that in my structs.. This is what I have tried, with the source JSON attached below. Any idea? Thanks!! JSON
MY STRUCTS
INSIDE THE VIEW
I can see this printed in the console, but how can I access each single element (e.g. main and weatherDescription?) [WebService.Weather(id: 802, main: "Clouds", weatherDescription: "scattered clouds", icon: "03d")] |
|
Since it's an array, you have to access it by index. The first item is at index So:
|
|
Thanks @roosterboy. I did try something similar but somehow I missed this syntax! Works fine now. Still feels like a very complicated way of going about this. Would you advise about using something different? (the rest of the app uses alamofire at the moment). Thanks! |
|
Well, it really depends on how you are using the data. Here's one way to do it that flattens and simplifies the JSON into a Swift struct:
Stripped of all the other code, it essentially takes this JSON:
and turns it into this Swift struct:
There are other ways this could be done, and if you are using the data differently—say, if you care about more than one entry in the |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
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.