|
Hi - I'm trying to create the 'Friendface' app on day 60 but cannot seem to pull the JSON data into the app. I used an extension for downloading JSON from Paul's tutorial 'How to Download JSON from the internet....' but although I managed to get it to work with his example struct, I can't with mine. I am assuming there is something wrong with how I've created it? I have tried so many versions: 2 separate structs, nesting one inside the other, used Identifiable, removed identifiable. I just don't know anymore. I may just be completely on the wrong track, I've never tried downloading JSON so just don't where to start. In terms of the contentView, I've only just created a button to test whether the data will pull through (it won't) and haven't gone any further than that.. Any guidance would be appreciated. Thanks
|
|
Hi, There are 3 problems i found, 1- the Type of the decoded user needs to be an array of users since it is what the URL returns.
2- the registered property in the User struct needs to be a String. 3- Since the registered property is a string the formattedDate wont work any more so you need to remove it.
Hope this helps. |
|
OK few thing need to change. Need a array to put the data in
Then in the method add
I change the
The whole file
PS you made to date optional but there is no need as every one has a
|
|
Brilliant! -Thanks @Hectorcrdna and @NigelGee - Of course it is an array -that makes sense now! |
|
Thank you @Obelix and all Unfortunately, still having some issues with accessing/formatting the data- although I have managed to pull the JSON data through I seem now to have hit another wall with formatting. The big problem I'm having is with arrays. I don't know either how to iterate over the tags array or access the friends array (which itself is made up of values from a nested struct). Separately, I tried to create a struct for 'friends' but then I could not get this to work either! The syntax below is wrong but hopefully you can see what I am attempting. Obviously, I don't want this ultimately to appear in content view, I'm just working out how to access all data.
The other problem is trying to access the friends array which is possibly even more complicated as it is made up of a another struct. I have tried to access/format this both from the nested struct (as in original code above) and also creating a separate one below:
This one was presenting problems in two ways. If I use as below -I get error messages -'Friends' required to conform to hashable but I cannot add the hashable protocol without getting another error message about not conforming to equitable!
If I use the other struct I get a result but it is not the friends data it is the id and name of the 'user':
This of course makes sense- as name and id appear twice within the JSON data, so I understand Swift needs to know which id or name I mean. Somehow I need to direct it first to 'friends' and then to id and name - but how? That I cannot work out. Apologies for the long post. Any help, further reading, pointers to previous lessons would be helpful. I have tried looking at Moonshot but can't quite get the answers I need. Thanks |
|
There are a number of ways you can do it depending on what you want to display. Make a computed property in the
Then you can add in the
or use a another
So now the body look like this (from previous answer)
User struct
|
|
@CJ identifies the very lesson @twoStraws wants his students to learn with this Day 60 project! Namely: Data you get from internet sources may contain many rows. And each of those rows of data may, in itself, also contain many rows of data. Your job as a SwiftUI programmer is to take the internet's data (thank you very much, sir) and consume it into your own application. But once it's in your application, it's YOUR data now. Slice and dice, extract and fold, crumple and dump it into any form you wish to meet your application's needs. @Nigel (above) gives a few examples how to slice and dice your data. Pay attention to Nigel! It's hard! But this is more of a logic puzzle than a SwiftUI lesson. Post-It NotesNot sure if you're a visual learner, but sometimes a white-board and coloured markers, or a few packs of Post-It notes can help. Or good old pen and paper, if you're old school. Lay out a few yellow post it notes on your desk, each with the name of a friend. Top-to-bottom you have
To get these names to display on your iPhone app, you picked the right technique: Then next to each friend Post-It, lay out a few blue Post-Its left-to-right, with their tags.
You're already listing each What's a View Factory?To help me adopt SwiftUI's declarative nature, I had to stop thinking of See -> View Factory Keep Coding! |
|
Thank you @NigelGee and @Obelix The computed property idea was helpful -I forget all the things you can do within a struct and use elsewhere in the program. @Obelix - thank you for your explanation - I'll be honest - I'm still completely stumped by how to access the data in the friends array. I'm not sure whether this was clear or not from my post but, from a technical point of view, I have no idea how to proceed. Currently I can only show data from the 'User' Struct and nothing from the 'Friends'/'FriendsData struct. I have tried nesting the FriendsData both within the User struct and also creating it separately but whatever I do I hit a problem as mentioned above. I just don't know how to find a path to the data. To follow on from your example, I feel that I would love to lay out the post it notes but they seem to be locked in the draw and I can't find the key to get them! |
|
Have you tried putting the code that I did in a project?
So to get one Friend
The only thing in this is the Add this to the
Are you trying to get the details of this one friend? ContentView
UserDetailView
Friend Detail View
|
|
@Nigel provides a GREAT example. Here's another that I worked on.
|
|
|
|
Thanks @NigelGee - To answer your question - yes, I was trying to get the data to appear in content view as a test to make sure I could access it. I won't put it there ultimately. I was actually thinking at one point of a ForEach within a ForEach but I had just never seen that done so didn't realise it was possible and so then started going round the houses and getting nowhere. I had to format the id as a string but that now works too. I hadn't got as far as making links to other views yet but many thanks for the examples given. |
|
Thanks @Obelix - I'm going to look at this a bit more in depth to fully understand what is going on - It is helpful to see different ways of accessing/showing the data. Regarding the ID - I did keep it as UUID but then added this computed property to the struct in order to show in text.
|
|
|
|
Thanks, yes, that's better - my solution was changing UUID to a different value - I have over-complicated this anyway I think as I could just use string interpolation. |
|
Sorry for the late addition to the thread but I have revisited this over a number of days and gradually trying to get to grips with it. One thing I am unclear about from the above example @NigelGee is the link to the DetailView and exactly what is going on in the code below - what are all the user/users relating to?- there are declarations of user/users in both contentview and detail view so it is hard to make out what is referring to what. Paul did something similar in Moonshot and I replayed the tutorial but he doesn't clarify why.
Also why is users declared as a private var in contentView but as a constant in detail view? Thanks again for your help |
|
In the
The
In [^1]: This can be done with new NavagationStack with iOS 16 (i am still trying to get my head around it!) [^2]: A whole different discussion on if it should be |
|
Thanks @NigelGee - This is helpful. I ended up changing the names of the properties to help me understand which part of the code was referencing which view/property. I get the idea, I think, that the code in Navigation link is like a conduit between two views, pulling through data and matching it up the other end. I won't worry too much about the specifics of properties at this stage, as long as they work, that's the main thing! Thanks again for your help - much appreciated. |
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.