Everybody what's going on this is Caleb with Deb's Lopes dot com.
And in this video we're going to add a few more functions to our download service Singleton that is
basically going to convert our array of dictionaries into instances of our repo model using those dictionaries
which is pretty neat.
And then we're going to set it up so that we can actually download that data pass it into some cells
it's going to be super cool.
Let's go ahead and let's start now.
Pull open that project.
And here we go.
Let's go ahead and below trending repots data.
Let's go ahead and actually stop that from running.
Let's close the console and you know what I'm going to make some space here so you can actually see
what I'm doing.
And we're going to write a function that is called download trending repo's and we're going to be able
to call this function here download trending repots directory and it's going to return Of course an
array of dictionaries.
Then what we're going to do is we're going to cycle through all those dictionaries and we're going to
pull out the individual data and then return an instance of repository.
OK.
Or repo I guess is the short name.
So go ahead and call phunk download WIPs download trending repo.
All right.
And just like before.
We're going to use a completion handler that is escaping.
So go ahead and type completion at escaping and if you missed my explanation in the last video go ahead
and watch it again back there.
Basically this handler is going to let us return values within a closure.
It lets them escape from the closure instead of getting stuck.
We're going to put that function here and that is the function that we pass those values into.
It returns an empty function but the return is not what's important what is is the array of repo's.
And as you remember we are not allowed to use named parameters in function types so we're going to go
ahead and use an underscore and use the internal parameter of repots array.
OK.
And it's going to be an array of type repo like so.
All right.
So that's great.
And that's what we're going to return as an array of type repo.
So let's go ahead and let's call.
Download trending repo's Diptera.
That's our function from this class and go and press enter on that completion handler.
This is the repo's dict array that we get.
So we're going to call this trending repots dict.
And now we have access to this property once it's done downloading.
We can go ahead and start parsing through all of those dictionaries and doing something with each one
of them.
So let's go ahead and let's just say for dict in trending repo's dict array.
Now we have access to each and every dictionary.
What we're going to go ahead and do is basically set up each set up.
Each property needed for instance of repo K and in order to do that I found that it's easier to use
a function that we can pass in a dictionary parse through it all and then return the repo that we need.
So let's go ahead and do that by calling phunk download trending repo.
And you know what.
This function should have an S for plural because it's going to return more than one download trending
Ribault with with no s is only going to return one function.
And so we're going to go ahead and use this function to return a repo like so.
And you know of course we need to basically download a trending Ribault from a dictionary.
OK.
And that's going to be a dictionary of type string and any like so.
But you know what.
We need an internal parameter of dep't like so.
And yeah so let's say that we pass in a dictionary like this one here we pass in a dictionary we're
going to go ahead and basically create the properties that we need.
So let's create an instance of repo so that we know what we need to fill in.
Let Ribault equals repo wups repo.
And we're going to need an image a name description number of fork's language number of contributors
and repo you are l.
So let's go ahead and let's think about what we need for the image.
We're going to go ahead and just say let avatar you RL And you know what.
If you remember in the GET have API we have an avatar you are l here and that's going to be the image
for the user that created this repository.
So we're going to use avatar you are ill.
And if you look you'll see that the key name is actually Avatar underscore your L So I'm going to copy
that and we're going to go ahead and say that that comes from dict and the key of course is named avatar
you are l.
Now that Avatar your all comes in as a string.
And so since we're using a dictionary here we're going to go ahead and just call as string.
We're going to force cast it as a string just so that it comes in as the exact type we need.
And now that's going to be used later on.
We can't yet download images because we have not written that function so we're not going to use it
yet.
But it is there.
So next we need a name so let's just go ahead and type let name and we need one T-nut too.
And that's going to come from dict K and the property in the actual dictionary is name just like you
might expect.
So go ahead and paste in name and cast it as a string.
Next we need the description with one T.
And that's going to come from dictionary.
With square brackets and the description.
If you go ahead and you look through we have the full name here.
We have a description at the swift programming language so I'm going to go ahead and just paste in description
and cast it as a string.
Ok that's cool.
And you know what since we're doing this we're going to go ahead and just let's give it the name let's
give it the description.
Now we need a number of forks.
Number of forks is going to be equal to dict.
We're going to put some quotes there and we're going to cast that as a string eventually.
And you know what.
All of these are going to come in as a string.
So I'm going to copy that and use that later.
But number of forks if you look you'll see that there is a property somewhere in here called forks right
here.
Fork's count.
So this is the number of times that swift in particular has been forked.
So go ahead and just remember that fork's underscore count K that's great.
And we need it to be a string so we're casting it as a string but that's not actually going to work
because the type that comes in is actually an INT.
And so we would cause problems if we tried to cast that as a string.
So what we're going to do is cast that as an integer.
And now we're going to move on to let language equals dictionary and in quotes we're going to go ahead
and look for language somewhere in here I believe it actually is up at the top.
Maybe not
OK.
So language is here it says that it's C++.
That's obviously not correct but for the most part it will be correct.
So just call language and that's going to be a string.
Then we need a number of contributors
K and that whip's contributors and that's going to come from our dictionary.
The contributor's do you see anything here for a number of contributors I don't.
And that's because that's not publicly available on the front page.
But there is a contributor's you r L and K now what we're going to do in the meanwhile is we're just
going to go ahead and pass in a value for a number of contributors and then later on we're going to
go ahead and write a function that can download the contributor's data.
So let's go ahead and set number of forks.
OK let's go ahead and set language and for a number of contributors let's just set one two three for
now and then you know what we can set the repo you are I'll as well.
So let's go ahead and type let repo you r l and that's going to come from the dictionary.
And the dictionary.
If you look for Ribault you are l.
We got a property right here called HVM l u r l k and that is where we get the get hub link.
So h t m l u r l That's Type that in h t m l u r l and of course that's going to get cast as a string
and passed in here.
Wups Ribault you are perfect.
Now that's great.
But for the image we're going to go ahead and type you I image and we're gonna call you I image named
and we're going to pass in a name.
Now if you remember I don't so that's why I'm going back to look.
We're going to use search Eikon large just for now and then later when we actually populate it with
a real image we will use that image of course but we're going to go ahead and unwrap that and get rid
of this number of contributors for now until we actually need it.
But then we're going to go ahead and just return the repo because this function is supposed to return
a repo for us.
So let's go back to our trending repots function here.
And we just went through and we set up every property needed for a repository.
It is a static repository for the image and for the contributors but everything else is coming in from
the API which is cool.
So in this function Here's what we're going to do.
We're going to go ahead and delete this Babai and we're going to go ahead and call self download trending
repo from dictionary and voila we have one dep't right here from our for loop.
But remember this function returns a repo.
So this itself is a repo value.
So let's just go ahead and call this repo and set it to be equal to our return from our function and
we need a way to kind of keep track of this because we're going to be returning an array of repo's.
So let's create a property at the top that we can just start appending these repos to as they come in.
Let's just say var repots array equals array of repo and we're going to instantiate it.
And once we create one from our download trending repo we're just going to say repots array append.
And we're going to pass it the repo just like that.
But when we're finished we need to basically tell it that we're finished and return our repots array.
So go ahead and just call completion and pass it the repots array.
OK we've already set it to be 10.
We're going to return 10 and upon completion we're going to basically just say all right these replies
are good to go they're done.
So let's go ahead let's go call this and let's see if we can get that data.
Let's just see how we did.
So let's call it download service.
Instance whip's instance and we're going to just say download trending repots completion is an array
of repo so let's just say return or sorry repo array.
And we're going to go ahead and just print reape it well let's see it's an array of repo's so that we'll
just print out a bunch of information.
Why don't we print out the first Ribault and then let's print out.
Well that should print out all the information so we'll just print out the first one that comes in.
So go ahead and build and run.
Let's go see how we did.
Here we go.
It's going to build.
It's going to download all of our stuff and we'll see if we did all right.
OK.
Take a look it says fatal error index out of range.
So that means that we're not getting anything back.
Let's go ahead and let's build and run again let's make sure that we're just getting an empty array.
That's probably the problem here.
Here we go.
OK.
Check it out.
We are getting an empty array and it does not like what we're doing so avatar you are l it is saying
we are not getting a value for Avatar you are l it's coming in as nil.
So you know what we're not actually using this.
So let's go ahead and let's just blank blank that out like so and let's try running this one more time.
Here we go.
Let's see if that helps our problem k we get an empty array that's great.
OK so it appears that not every repository has an avatar you or else so we're going to have to do something
in a future video to basically only download repositories that do have an avatar you're L..
But we are getting a bit of a problem here because we are not returning any values and that's because
again we're completing this outside of our closure.
So let's go ahead and put our completion at the bottom of our four loop here.
Let's build and run.
And that should successfully return our repots array.
So let's go ahead and let's build it let's run it and let's see if we actually download data.
OK.
Look at that.
So we're printing out the repo's array but it's just showing that these are instances of repo.
So since we're getting all these repo's Let's count them one two three four five six seven eight nine
10 11.
Interesting.
So we're actually getting 11 back o.
Right.
That's because of zero indexing so if we want 10 Let's go hadn't asked for nine.
All right.
So download service trending feed Visi let's print out the first item in the repo array and let's see
all the values that we get when we do that building and running one more time.
And here we go.
OK of course sorry.
Let's actually print out the values.
How about the name.
Print out the name.
OK.
That'll make a little more sense.
All right it's building.
Here we go.
And we should get swift.
There we go because remember in Safari here Swift is the first one that we get.
So we're downloading 10 repositories and they happen to be coming in exactly as we want.
We are so so close guys in the next video we're going to actually write a function that will download
the avatar image.
We're going to download the contributor's data and we're also going to make sure that all of the repositories
that are coming in have all the properties that we need.
Otherwise we'll just kick them out.
So we're going to go ahead and delete this.
We're going to go ahead and head back to download service and in the next video we're going to make
it happen.
Let's go do that.
Let's head over to the next video and download all that good stuff.