Everybody what's going on this is Caleb with slopestyle.
And in this video we're going to talk about observables and observables are sort of the foundational
building blocks of our acts and our swift and so in order to begin.
Go ahead and pull open your tricks playground.
The one that we built in the last video and before we began we're actually going to go ahead and select
all these other playgrounds because we're not going to use them.
And I'm just going to actually move them to the trash sometimes X code gets grumpy about that.
But all you need to do is reopen X code as you can see it just crashed.
And I'm going to go ahead and pull it open again.
There we go.
And now we have just our singular playground.
So let's go ahead let's start by talking about observables.
Now everything in our ex-wifes is either an observable sequence or something that subscribes to events
emitted by one of those observable sequences and that might be a lot of jargon and a lot of words maybe
you're not familiar with but I'm going to show you how it works.
We're going to start by creating our first observable case so let's just go ahead and type let number
sequence equals observable and in order to create an observable for a single value you can type observable
not just meaning just this one thing.
And let's go ahead let's pass in the number five.
OK.
So now we have an observable with a value of 5 K that's pretty cool and the coolest thing is that it
can infer what kind of observable this is so I want to show you one other thing you can type maybe let
number equals observable.
And using these brackets you can go ahead and set it to have an explicit type.
But the cool thing is with swift type inference it allows us to infer that type automatically.
And of course you could have gone in and you know done just what we did here with an integer not just
that would create the same thing but no need to if Swift can infer it.
So very cool although it is helpful to know if you need to create a specific type of absorbable maybe
for C.G. float or for another type of value.
So that is how we're going to create an observable.
Now I want to show you how you can subscribe to this observable and do something based on how the values
change.
Or in this case how they have been added.
So let's go ahead and let's say let number observer or maybe subscription.
And we're going to set that to be equal to number sequence.
But what we're going to do is we're going to call DOT subscribe.
OK.
And we're going to subscribe to number sequence.
That means that we're setting up a property that's going to be able to monitor what happens in this
observable sequence.
But there are a couple different things we can do upon subscribing.
OK so I want to show you that you can see here subscribe is on NEXT ON ERROR uncompleted and on disposed.
Now on next basically means when a value comes in.
So if I add five here on next would be called after on next is called If there's no error on completed
will be called.
And so I want to show you how these work what we can do is for.
Well you know what let's just do on next first.
And so I'm going to actually get rid of all of this and just type on next.
And then we're going to do a closure here.
So basically on next we're going to print whatever the value is and in order to get into that we can
use the dollar sign zero.
And what that's going to do is it's basically creating a temporary variable and it's cycling through
this sequence of one value.
It's only a singular value and I'll show you how to make an observable of multiple values in just a
moment.
But for now what we can do is we can print out whatever is in here.
Now you can see it prints out five down here at the bottom.
What if I were to change it to 10 it would print out 10 k.
So observable that justice for making an observable of a single value.
And we have subscribed to it.
Now did you notice the number sequence by itself doesn't do anything.
Only when we subscribe to the sequence and call on next or on completed or on disposed will it do something.
Now that is really interesting to know.
And we're going to see how some of these other capabilities like on completed and on Error work.
So let's create a new sequence.
Let's go ahead and type let.
Hello.
Sequence equals observable.
And we're going to use a new function.
We're going to use observable from.
And as you can see it's looking for a sequence or an array.
So I'm going to go ahead and select the one that's looking for an array.
And we're going to give it one.
So type in some square brackets there and we're going to create a string for each character in hello
like so h e l l o.
And so now we have an observable that is observing this array we have created an observable from an
array.
That's what from as for now in order to do something with this we need to subscribe to this observable.
So let's go ahead and type let Hello subscription wups subscription equals Hello sequence that subscribe.
But what we're going to do is we are going to basically use our subscribe on call here and we're going
to switch through all the different cases that can happen to see what happens in a normal subscription.
So go ahead and press enter and we're going to call this event and we're going to make a switch that
will switch through all the potential outcomes.
So go ahead and type switch event and we have some cases here.
So type case and let's see what is possible for us to access we can access next.
We can access error.
Okay.
Remember on next on air case completed Okay.
Now for each of these cases we need to do something.
So for case next we're going to go ahead and print but we need to know what to print.
We're going to create a variable here or a constant rather that can capture the value of whatever event
is happening so go ahead.
And in parentheses here we'll type let value and we can print out value.
So remember on next we printed out the value here and it printed 10 right.
We're going to do the same thing here except we're going to print out the individual characters in our
observable because remember it's an asynchronous sequence.
Pretty cool.
Now for Aero we're going to do the same thing we're going to type let error and we'll print an error
if there is one for completed.
We don't need to print anything we just basically need to know we're done subscribing so we're just
going to print what's inside the parentheses we'll print completed.
So now that this is done.
Watch what happens.
As you can see we are going through our subscription here and we are printing the value of every next
event.
So what is happening is we are going through this and next or on next is being called We're printing
the value so we're cycling through the first element in our array is H then E then L then Elvan o.
And then when it's finished we are printing completed so you can see that every sequence once it gets
to the end uncompleted is called unless there's an error and then the error would be printed which of
course you could do something with.
So that is just one way that these observable sequences are very very cool.
You get a lot of really great control over what you're doing.
And so these are just two ways we can create observables and we're going to be talking about different
kinds of observables in the next section of this course.
And they're called subjects.
They're like observables but they work in specific ways so some are better for some things and some
are better for other things.
Now this is just a basic intro to observables and we're going to talk a little bit more about them in
the next video and the video following that.
But by the end you're going to have a good understanding of observables as you can see we are observing
a certain value we're subscribing to that value and we can do stuff with the values inside by simply
calling dot subscribe.
Now in the next video we're going to talk about disposing.
And that's part of the memory management part of our ex-wife that we need to to be on top of.
But don't worry it's really easy.
So let's go ahead and head over to the next video where we're going to talk about disposing an observer.
I'll see you there.