Hey everybody what's going on this is Caleb with Debb slopes.
And in this video we're going to talk about our final transformation.
It's called Ziph and it sort of does exactly what it sounds like.
You can take two observables or three or four and zip them together into one single observable which
is pretty cool.
So let's go ahead and pull open our X playground just like so.
And what we're going to do is we're going to go ahead and create a new playground page.
We're going to name this zip and we're going to go ahead and select all delete and make a little more
room here.
Then of course we're going to paste in our four lines that we've been using and I just went ahead and
pasted the Dispose bag because we're going to need it anyway.
Then we're going to go ahead and create some observables.
OK.
We're going to create one for car makes.
We're going to do another one for car models and then I'm going to show you how zip works.
So go ahead and type Let car makes and that's going to be an observable.
But of course it's not showing up yet so we're going to go ahead.
Oh you know it looks like it did dot of 0.
Maybe not.
OK.
So go ahead and save it and close it.
Then we're going to reopen it and build it.
There we go.
And now if we type of IT SHOWS UP LOOK THAT.
So what we're going to do is we're going to go ahead and give it three string values.
The first is going to be a car model like DeLorean.
The second is going to be another or sorry not a car model a car make.
The second is going to be Bugatti very fancy.
And the last one will be Lotus.
OK.
So those are three car makes.
And now what we're going to do is we're going to create another observable but we're going to call it
let car models.
It's going to be an observable and we're going to use of to create it.
Now we're going to go ahead and give it three car models as well.
Now I'm going to put them in the proper order for now because that is important.
So I'm going to type DMC 12 that's the model from the future.
I'm going to go ahead and do the Bugatti Veyron and the Lotus Elise.
All right.
So now we have an observable car makes and an observable of car models.
Now we can do is we can call observable we can create a new one but we can call DOT Zipp and if you
read the description here it says merges the specified observable sequences into one observable sequence.
When ever all of the observable sequences have produced an element at a corresponding index.
So what we're going to do is we're going to do zip and collection.
Now what we need to do is we need to give it both of our observables so we'll type car makes Khama car
models so we passed in both and we want to Zipp those together into one single observable.
OK.
Now after Zipp we need to create some curly brackets here for the closure part of dot Ziph and what
we're basically going to describe is what we should print out from both observables zipped together.
Now what we've been using we've been using dollar signs zero right.
That is to get the elements out of an observable we cycle through and we pass the element to dollar
sign zero.
Right.
That's what that's what we do.
But we're dealing with two observables.
So if zero is the first index what we can do is we can print out dollar sign one and that will get the
element at the second index which in this case is car models.
Now this is no good we're getting an error it's saying that consecutive statements must be separated.
But what we're going to do is we're actually going to just go ahead and add an empty string with just
the space right in the middle.
Well I guess it's not empty but it has a space.
And what that's going to do is it's basically going to print out the first element from the first observable
then put a space and then print the first element from the second observable.
OK.
So dollar sign one is for our second observable dollar sign.
Zero is for the first observable.
OK.
So of course this is an observable but it's worthless unless we subscribe to it.
So go ahead and call subscribe and we need to use on next whip's on next like so then inside of next
we're going to just print and you might be thinking well do we print dollar signs zero dollar sign one.
The thing with Zipp is that zip is going to basically take our two observables and smoosh them together
into one observable which is really cool.
Now because of that since it's just one observer we can use the first index like so and watch what happens
when we print it out.
We get DeLorean space DMCE 12th Bugatti's space Verin Lotus space Elise we get them zipped together
into a single observable.
That's pretty cool.
And you know what I think I'm actually going to clean this code up a little bit.
Sometimes things like this can exist on one line and it looks just fine.
And then we can call the DOT subscribe underneath.
Very cool.
Now of course the last thing we need to do is to add it to the Dispose bag like so and go ahead and
return down to its nice and clean.
But check out what we're doing here guys we are basically creating two observables with three values
in each we are calling observable Ziph and then we're using car makes car models and we're zipping them
together into a single observable.
OK.
Now what this is doing is it is zipping through and it's saying OK first observable first element then
a space second observable first element then zips through again.
Second element in the first one second element and the second one then third and third and it prints
them all out on the next call.
That's really really cool.
Now I want to show you what happens if I add a fourth element.
You might think oh there's going to be an issue it'll say maybe the index is out of range.
But watch what happens if I did say Chevy or Chevrolet.
Watch what happens.
It just prints out the three.
Basically what Zipp does is it merges together to observables but it pays attention to the smallest
index.
So you never have an issue with it being out of range because it can never go past the smallest number
of elements in an observable which is pretty cool.
Now of course if I were to add in suburban You'll see it'll print out that fourth element now.
So that's one thing we can do but just know that zip is going to pay attention to the observable with
the smallest number and then it'll make all the pairs of elements that way.
Now of course pairing specific data like this is probably not the best because your two observables
may be unsorted data but it is good if you want to pair them together.
Maybe if you wanted to create an observable of two sets of data that are chosen on the same screen in
an app if you maybe they pushed a button for next it could merge those together into a single observable
and then pass it on to the next controller.
That's just one idea I had for how this could be used.
So that's zip that's one of the transformation functions in our swift and the next video.
I'm going to give you a challenge to put your knowledge of transformations to the test.
Awesome job with these past four videos and I'll see in the next one.