Learn Courses My Dashboard

I was trying to make stock app but there is a problem please help

I was able to pull the data from remote but then couldn’t make the view

[https://we.tl/t-mfyduCshXE] (https://we.tl/t-mfyduCshXE)

here is the link for my project

Well that was a bit of fun to get working.

Have a look at this and see what you reckon. I’ve messed around with your code a bit (hope you don’t mind) and changed a few names of the properties here and there but it sort of looks like what you had.

Anyway it now retrieves a stock item and you can see it on the screen.

Thanks for replying and looking into it. I am terrible at naming but now I’m checking it

I understood it Thanks. so we had to use the uuid() because the json repose was array, RIght?

Because of such nice people like you I can also see myself getting successful one day.

Just glad to help out man.

OK I added the var id = UUID() so that when you use the array in a List or a ForEach, the contents of the array conform to Identifiable which means that the SwiftUI “View” can differentiate each element of the array uniquely. If that doesn’t make sense right now, just stick that in the back of your mind for the time being because as you gain more experience it will all make sense.

A lot of JSON API’s do have an id field so that makes life a little easier. That field could be a simple Int rather than a UUID and still conform to Identifiable.

One last thing I want to know that, when I was reading the documentation the https request was there So I followed , in your code you skipped that part can we skip it on every app we make in future

request.addValue(“application/json”, forHTTPHeaderField: “Accept”)

The url you are using has all the necessary information in it to perform the API call and get a result back. If you place that url into a browser window you get a result back so there is no other information required despite what the documentation says.

I guess I work on the principle of “just enough should do the trick”.:stuck_out_tongue_winking_eye:

1 Like

Hey may I send you another project I was working on same thing happen with that one too?

If you have time please look at this as well . I’ll be really grateful

OK that one is working now.

I changed some of the naming that you had in your struct. Your struct for Articles should be Article since it describes ONE article. Your News struct now more accurately describes your data in that your articles is an array of Article, ie let articles: [Article]

In general I use let rather than var but there are really no hard and fast rules governing the declaration method.

Your enum declaration must show the name as CodingKeys. You can’t have codingKeys.

In your NewsModel I took the code out of the init() and placed it in a function. You will find that in future when you need to do more than one thing in the init() it is better to call a function to make your code easier to read.

I changed your var at the top to @Published var articles = [Article]() since all you are interested in is the news articles. In the Network call the result is News which contains an array of articles so all you need to do is specify that self.articles = result.articles

I added some code in ContentView so that you can see that it works.

Edited project: Dropbox - NewsAppEdited.zip - Simplify your life

1 Like

Thank you , God bless you. :slightly_smiling_face: