So far when we talk about Jason what we have been able to do is to get agents and file input in our
projects and then pass it that way.
That works fine but most of the Times Jason as we said before we talked about Jason API is intention
usually is to have an API application programming interface essentially is just a way in which US developers
can communicate to a web server which will be serving as some data from database somewhere.
So usually it's in form of an API application programming interface that allows us to get that data
as opposed to having to rely on getting the actual hard code file and put in our projects because that's
just not always the case and it shouldn't be.
OK so how do we then parse data that it's coming from an API.
Well we're going to go ahead and create a new project.
I kind of have a head start.
I'm gonna go ahead and say next.
So I'm going to give it a name.
I'm going to call this pass.
J Just like that next.
And we know exactly where it's supposed to be.
And while I should have a new project looks real good.
So what is it that we're gonna be passing.
Well so there is this website called Jason Place holder.
As you can see here where it gives us a very simple API.
In fact they call it a fake API.
Jason bozo holder.
It's a fake API that allows you to just play around with.
Jason now there are different end points here.
The first endpoint endpoint meaning a place where we can actually go and fetch Jason code and you can
come down here you can see that there is an example you can click try.
And it shows us the actual payload payload in this case suggests the adjacent data.
Now I have to remind you that this should this one remind you of swift dictionary as we've talked before.
If you have been following us so this will come in handy.
Now one thing to remember is that the way the route of our Jason is constructed is very important because
we will see later.
Why is that so.
What is the route.
The route is always what initiates our API.
What I mean by that is that while in this case we know that the route is going to be a dictionary because
we have these curly braces.
So if we go back down here we can see other and points of our API.
For instance if I go to comments you notice right away we see that we have a different structure.
So at this point here as you see that we have what we call an array let's make this a little bit bigger
perhaps.
So you can see there we go.
So we can see it at the top here we have different structure.
So these are brackets which symbolize an array.
This means that our API.
Now the route is indeed in a right.
So this array contains objects or dictionaries.
In fact you can close down like this and open him up.
This will become more clear once we get to passing and so forth.
All right.
So back to our Jason Place holder here.
This is the end point that we're gonna be using to pass of course this OK so let's go ahead and copy
that the euro because we're going to need that.
And let's take us back to our X code so we can go here and say Right click we want to create a new file
let's create a sweet file there and we're going to call this net working manager such.
All right so we have it.
And get rid of all of this stuff that we don't really need.
OK.
So what we're going to do here is we're going to go ahead and create a class called net working manager
and this class here will have to confirm to an observable object because we want to make sure that we
have listeners who will be listening to this network manager.
One moment that we get the data all the listeners will be able to know is we'll be listening right to
this object here that hey there is data.
Something is changed and then we gonna go ahead and revamp our UI or this case will draw our user interface
with the new data.
OK.
So the observable objects are going to say observable.
If I can type of course observable object at such close I want it down there.
And so here is where we going to be doing the coding.
Now before we can go forward here we need to go ahead and create a data list entry.
So these data list entry it's going to map our Jason API payload or object with an actual swift y object.
So what I mean by that.
Let's go ahead and create it real quick.
So Nicole this is going to be a swift file and let's call this data list entry.
Very good.
Good call whatever we want but it seems to be good that name there.
So I'm going to go ahead and paste in one of the example of the payload that we have.
I showed you earlier.
So we have the user I.D. we have the I.D. title and body.
So this is where we expecting to receive from our Jason API.
So this model class can create.
We'll have to map all of these properties so that we can then have those as Swift file.
So here I'm going to say struct data list entry.
And this is gonna be decoder ball as well I's identifiable.
So I'm going to confirm to two different protocols here because we're going to need those in a little
bit.
Okay.
Let me close that one down like this.
Okay let's go ahead and close this down.
Let's go ahead and add a few properties here.
So I say var user I.D..
Notice these properties have to be exactly the same properties as we are expecting.
OK.
So user I.D. is going to be an integer var I.D..
It's going to be also an integer.
Var title is a string and far body.
So it's going to be a string.
So essentially all of this.
One two three four.
All right.
Now that we have that go back to our network manager here because we will use our data list entry here
to pass the data.
Let's go ahead here.
I'm going to say var data list.
It's going to be expecting of course an array that will contain data list types.
That makes sense.
We're going to use this data type variable here which expects to be a data list entry to actually populate
our data but of course we have to instantiate it here so I'm going to go ahead and put something like
that.
OK.
So we actually creating new empty array.
We expect to actually have a data list entry types.
The thing here to notice is that because this data at least here is going to be dealing with data that
we're getting from our API this is where we are making sure that these variable indeed can listen and
publish.
In this case actually are the events that happened.
So meaning once we get the data and we populate this data least with the information from our API we
want to make sure that the swift UI knows how to publish this event to any other struct or view that
needs this data for that to happen we have to save at and pass published just like that is that at least
it will be able to publish the data and all the changes that happened to this data to anybody else who
is listening.
OK.
So this is good.
Way of data binding if you will so that the views are actually changing as data comes in so that it
changes the views that are listening to our data list here or our network manager as an observable object
they will know to update their user interface with the new data.
OK.
So let's go ahead and do the actual coding.
So what I'm gonna do here I'm going to create any net initialize are here and I'm not gonna pass anything
inside but I'm going to start as a variable as a guard to let to you or L so anytime you are dealing
with or else we need to make sure that our your l is actually guarded if you will.
Just in case things go wrong.
Something I say is equal to your l I'm going to make sure that your out is actually valid when I'm passing
the string here and the URL.
We are going to be hitting in the end point of our API we're going to be getting is here HDP as Jason
Place holder.
OK.
Type code dot com posts all right.
That's where we are getting everything.
In fact if I go again to this link here you can see this is what we are going to be getting right.
All right so if this goes wrong meaning if or Al for some reason is wrong or something didn't go right
I'm gonna go ahead and pass an else here and just say return like that.
So if anything goes wrong we're just gonna ignore everything and move on to the next statement.
So next want to say your RL session such and I'm gonna invoke the shared Singleton in this case here
when I invoke the data task with your rules so I'm gonna pass the URL that we have there.
So these here will actually allow us to do all of the begging passing getting stuff.
So these here what are we doing we are putting everything in the back end not in the main thread so
we can go ahead and hit this you l here API and start the process of extracting information from it.
So then we're going to be expecting a few things from our data task.
So the first one is data which is will contain the payload Chase on persay in a different format and
then we can ignore anything else with underscores like that when I say in some closures and then I'm
say guard again just protect ourselves like data that we're receiving say data.
Else if something goes wrong again when I just say return like that.
Okay.
So in this case here we're going to make sure that if data is actually something usable Of course we're
going to put inside of this variable here.
Else let's just move on.
So now that we have that data I'm going to say let data list.
OK.
I'm going to make it a try here just in case things don't go right.
And then I'm gonna invoke the sun decoder because we need to decode whatever we're getting into something
that makes sense for Swifty y.
So Jason decoder I'm getting to decode method.
Notice 40 decode method we have to pass the actual type that we want to use to decode.
Because remember what we're trying to do is to map our Jason payload the object we are getting with
these data list entry.
That's why we need to pass some sort of the type that we want to map with in this case here.
The type we want to map it with is going to be of course an array that will contain data list entry
has to be exactly the same thing as we did here.
OK.
Well from what it's going to be from our data which is this one that we're passing that's extra spaces
there now to finalize this we have to say that self want to say we want actually to go and get the data
type not the object itself.
Very good.
So once we have that we're going to say dispatch.
Q Now the dispatch queue here allows us to manage the execution of our task.
Right.
Could be serially or concurrently in our apps main thread or in a background thread.
So I'm going to use that for our advantage because I want to make sure that everything happens in the
back and I'm going to go to main at async because I want to make sure that this happens asynchronously
when it passed the first one closure there.
And then in this case here once we have our data we're going to populate our data list that stadium
because now we have the actual data list that we created here which contains everything mapped out for
us to start using in our code the data that is when say self that data list meaning this guy here which
is a published right.
And I'm going to just add what I have to it is going to be equal to data list which is that it lists
is this one because at this point here they said at least has the actual Jason which we decoded to look
like an actual swift object which we can then go and fetch all of the properties.
OK.
And before we forget we actually have to invoke your resume method which resumes the task if God forbid
if it's suspended.
OK.
And here inside here we can use all sorts of things.
I mean just go ahead and print for ourself that data list which is this guy here which of course should
have all the payload meaning again should have all of this.
OK very good.
So if we go ahead and run right now of course nothing is going to happen because we haven't actually
invoked this networking manager which we're going to be doing in the next video.
So everything should at least the work for now and a next video we are going to see how to invoke this
network manager instead of our kind of view so we can hopefully see something.