Hey everybody what's going on this is Caleb with deps Lopes dot com and in the last video we were having
some trouble we were having a crash because one of our values was returned as nil.
And that is a problem.
So we're going to go ahead and figure out the source of this issue.
So let's pull open our escort project and I want you to take a look here.
We've got a crash on contributor's.
You r l k.
We're trying to access the property here contributor's you are l.
And if you look at the get hub API that is the name right contributor's underscore you are l So what
is the problem.
Let me show you.
This is the danger of doing things strongly using lots of strings.
Really we should be casting all of these as their own constants like we did with the elves here to make
sure that we don't miss typed them ever.
But we're going to go ahead and we'll have to do that and in a later video to fix that.
But for now check it out.
Contributor's you are l and watch what happens when I create this originally contributor's you are l.
That's great.
But look when I create the dictionary individually I forgot the ass contributor for your L-1 single
contributor.
So without the apps this dictionary does not know how to find this because this key doesn't exist.
So I added the s right there.
I just added it and let's build and run let's see if this fixes our problem one single letter could
have been the cause for that entire crash.
Let's go check it out.
Here we go.
And if you remember in our trending feed Visi were trying to print out the image the way that we the
way that we want.
So repots array the first one the image and print out image size.
But check it out.
Index out of range.
Fatal error index out of range so it looks like that is not working as well.
So it looks like it's not allowing us to return our repo the way we want.
This is coming in as knows well trending repots array.
For some reason is coming in as Knill.
Interesting and let's see that's from Alamo fire itself.
So let's think about what we're doing here.
Let's go into our download service.
Let's close the console there let's stop the app from running.
And let's think first we downloaded all the data and that's good that comes in properly we know that
already.
Okay.
And you know what.
Actually we have changed it since we first ran it.
So let's go ahead and head back to trending feed Visi let's comment this out and then let's call download
service that in that instance got download trending repo's dict Auray we'll just say dict are notes
in array so I guess we'll just well dict array both.
So we're going to go ahead and print dict array and let's run this let's make sure that we are getting
the data we want whenever you change these functions.
It's important to test them to make sure that they work the same way that you expect.
So here we go pulling out downloading.
And here we go.
Okay good.
So all the data is coming in the way we want.
That's perfect.
Very cool.
So that one is good.
Let's go ahead and head back let's look.
When we get our trending repots array that works the way we want.
Perfect.
So then downloading trending repo's we pass in the repo's directory from this other function.
We go through we call it download trending repo.
All right we pass in the dictionary we return the repo.
Then we append it.
OK so let's go ahead and let's call that one and let's see how that works download service instance
download trending repo's.
And here we go we get returned repo's let's just print.
Whoops returned repo's and let's just print out the first one and let's print out the name.
OK let's just double check make sure everything's working all of the data is getting transferred the
way it's supposed to.
Let's identify this problem.
Here we go.
OK.
So it looks like this may be a part of our problem here.
Let's go back let's look.
All right.
Trending repots array is not being able to be not being able to do what it's supposed to do.
So let's figure out why we are calling.
We create our repulsor right here we download the data we know that works.
So our for loop is good as well because we're cycling through all of them.
Download trending Ribault we pass in the dictionary.
OK that's good.
And then let's look in download trending repo to see.
We passed in the dictionary.
We create all these properties.
Avatar you are l contributors you are all named description fork's count language.
Tim you r l k let's make sure everything else is good here.
Name description language avatar you are l that looks good.
OK.
Index out of range.
This makes me think that perhaps we are asking for one number somewhere and then asking for another
number elsewhere.
Think think think so.
We are passing in our dictionary here.
When we pass in the dictionary we get all those values.
We download the image we download the contributor's data we create a repo and then we then we return
that singular repo.
That's good.
OK that's great.
So let's go back and let's look at this download trending repo's.
That looks good.
OK so it looks like we're downloading everything we're going through all the dictionaries appending
it.
But we're still having some trouble here.
OK so how about this what if oh ok so how about this.
I think I know what the problem is we are going through.
And once we get into this closure we can't escape.
So let's try pasting our completion handler inside our for loop.
Well you know I think that'll it'll return a new array every time it's not very efficient.
Let's see if it fixes our problem though because I don't think we're really out of range.
I don't think that's the problem.
OK let's go ahead and see where we go.
And we are calling trending repo's right.
Yes.
Download training repo's.
Very cool.
OK.
So we're still getting an issue here.
We're not getting the functions back that we need.
So this is not good.
Let's go ahead let's put some breakpoints in here to see what the problem is let's make sure that we're
actually getting at least one.
So let's build and run.
Let's see if we're getting any data passed in into this function.
It's going to build and run Riego.
All right.
OK.
So interesting.
This is causing a problem our breakpoint is never actually getting called so that's extra tricky.
Let's go ahead and let's think about how we could fix this.
Well we could count against the number of repos coming into our array and then once it gets to a certain
amount we could return it.
Let's let's try that out.
So inside self download trending repo.
Go ahead and call if repo re count is less than nine.
Let's go ahead and think about what we're going to do if it's less than nine.
We want to append it.
OK.
But if it's not less than nine we want to go ahead and call completion because that is when we will
know that we're finished.
OK.
That's pretty cool.
I will I think solve our problem here.
Let's go ahead and let's run the simulator and let's make sure that we're not going to run out of our
index.
That's what we are doing we're currently out of index but we want to fix it.
So here we go launching versey app.
Ok so that is good.
We are getting the information we need.
Looks like it's working now.
So we returned the first item in the repository and that's great.
That's super cool although you know what I think let's let's look back at our download service.
We're saying if the repo's array count is less than 9 but if I remember properly the repulsor rate can
go up to 10.
So let's set that to actually be 10.
Let's run this again because it looks like we're missing our first element because Alamo fire if I remember
properly let me look.
But I think it was the second element.
First we get swift and then next we get allemande fire.
So that shouldn't be the item at index 0.
But let's go ahead and let's build and run let's see if adding that extra one is going to give us what
we need.
It's running through doing all of its downloading and we're not getting anything printing just yet.
Ha.
OK so it looks like it's not going to return.
Let's let's go ahead and just leave it at 9:00 for now and then we're going to go ahead and actually
modify that later on for now.
At least we're getting some data returning.
We might need to do some bug fixes later.
But guys this is great.
We made a fix we made a change and sometimes in programming that what you have to do.
You have to make some changes to make it work.
So I'm building and running one last time just so that we can see all the values that get returned and
let's just let's check it out.
OK so cool swift algorithm club.
Interesting.
So it appears that these are kind of coming in in a random order as if maybe like an image that's coming
in is larger than another so it's taking longer to download so another one that's quicker is going ahead
of time.
So you know what I actually want to modify the repo's array here.
Let's go ahead and just type let sorted array and it's going to be equal to repo's array.
But we're going to call datt sorted and you'll see that you can basically compare to repo's and then
do something depending on the sorting.
So we're going to just press enter and the first one we'll call repo a.
The second one we'll call repo B.
And let's just say if repo a.
If repo a dot.
Well we're starting it by number of forks.
So let's just go ahead and say If number of forks is greater than Ribault B.
Number of forks we want to return true k and otherwise we're going to return false.
And the reason for that is basically the sorted array is going to sort and organize things so it's going
to take the first and the second items if one is greater is going to return true and that'll be at the
top of the list then it's going to compare the next one.
And basically what it's going to do is it's going to look through all of them and sort them and then
return them to this nice beautiful array.
Then once it's all sorted we're going to return the sorted array sorted array.
Beautiful that's much better.
So go ahead and build and run.
And let's see if that helps.
We should get swift as our very first item because that's going to have the most forks.
So let's go ahead and run it.
And we should get swift to print out the way we want
go loading doing its thing and there we go.
Swift is the first item because we have now properly sorted the data.
It looks like it came in the wrong way so we had to sort it ourselves which is beautiful.
But guys it's working now.
We're going to move on to the next video where we're going to start putting these functions together
to start making data show up in our table view cells.
So so cool.
So let's go ahead and let's head over to the next video and we're going to we're going to do that now.