|
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 |
SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.