|
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 |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.