1
00:00:08,080 --> 00:00:11,230
Hey buddy what's going on this is Caleb with Deb's slopes dot com.

2
00:00:11,260 --> 00:00:14,740
And in this video we're going to be talking about variables.

3
00:00:14,770 --> 00:00:19,450
Now you're probably thinking wait variable's that's a really basic easy component of Swift I already

4
00:00:19,450 --> 00:00:20,390
know variables.

5
00:00:20,680 --> 00:00:21,670
Not like this you don't.

6
00:00:21,670 --> 00:00:29,320
In Arek Swift a variable is actually a custom version of a behavior subject which is a custom version

7
00:00:29,380 --> 00:00:35,940
of an observable so a variable in our X Swift can actually be used just like an ordinary variable.

8
00:00:36,160 --> 00:00:41,920
But of course you have the same subject functionality that you do with behavior subjects and published

9
00:00:41,920 --> 00:00:44,820
subjects but it works in a little bit different of a way.

10
00:00:44,830 --> 00:00:46,140
So that's why I'm going to show you.

11
00:00:46,210 --> 00:00:52,450
So go ahead and pull open your Rick's playground and we're going to go ahead and copy those three top

12
00:00:52,450 --> 00:00:54,620
lines that we've been using over and over again.

13
00:00:54,880 --> 00:00:59,180
And we're going to right click on behavior subject and create a new playground page.

14
00:00:59,260 --> 00:01:04,150
We're going to name it variable and then we're going to go ahead and select one of the numbered lines

15
00:01:04,150 --> 00:01:04,710
here.

16
00:01:04,930 --> 00:01:07,150
Select all and delete.

17
00:01:07,150 --> 00:01:13,930
Then go ahead and paste in your import our swift import playground support as well as the needs indefinite

18
00:01:13,930 --> 00:01:15,030
execution line.

19
00:01:15,220 --> 00:01:21,510
So that's cool but if you build it if you clean it let's clean it there.

20
00:01:21,670 --> 00:01:26,640
You might notice that it will say that it cannot access our X Swift.

21
00:01:27,070 --> 00:01:28,800
Let me go ahead and give it a shot here.

22
00:01:28,810 --> 00:01:31,060
Let me see if we actually do have access.

23
00:01:31,260 --> 00:01:31,640
OK.

24
00:01:31,660 --> 00:01:33,440
So observable is not showing up.

25
00:01:33,460 --> 00:01:37,310
That gives me a clue that we do not have it properly imported.

26
00:01:37,510 --> 00:01:41,230
I don't know if you see that that error message at the bottom is saying no such module.

27
00:01:41,230 --> 00:01:43,660
So let's fix that like we have before.

28
00:01:43,690 --> 00:01:45,260
I'm going to save this.

29
00:01:45,310 --> 00:01:47,630
Close it and reopen it.

30
00:01:48,690 --> 00:01:51,770
And as soon as it opens up we can go ahead and build it.

31
00:01:51,930 --> 00:01:55,860
And now we should have access to our observables.

32
00:01:55,860 --> 00:01:56,660
There we go.

33
00:01:56,870 --> 00:01:57,300
Cool cool.

34
00:01:57,300 --> 00:02:01,720
So that's just a really easy way to clean out the project and get it set up with swift.

35
00:02:01,740 --> 00:02:06,590
So we're going to create a variable which is a type of observable.

36
00:02:06,600 --> 00:02:09,750
Now go ahead and do that by typing var variable.

37
00:02:09,750 --> 00:02:14,430
Kind of redundant equals variable even more redundant.

38
00:02:14,430 --> 00:02:17,220
And we need to give it an explicit type.

39
00:02:17,220 --> 00:02:24,170
So using the brackets go ahead and pass string in so that it knows that it's of type string.

40
00:02:24,180 --> 00:02:30,570
Now a variable is just like a behavior subject in that it needs an initial value to begin.

41
00:02:30,570 --> 00:02:35,580
So think about it it's just like an ordinary variable if you're going to use a variable that has to

42
00:02:35,580 --> 00:02:36,730
have a value.

43
00:02:36,780 --> 00:02:41,440
So go ahead and pass in a string here saying hello.

44
00:02:42,060 --> 00:02:46,970
And now we have a variable observable that has a value of Hello.

45
00:02:47,110 --> 00:02:47,620
OK.

46
00:02:47,670 --> 00:02:49,080
Easy enough.

47
00:02:49,080 --> 00:02:56,550
Now what we're going to do to use it is different than behavior subjects and publish subjects a variable

48
00:02:57,240 --> 00:03:02,100
in and of itself does not register as an observable because it can be used in a couple of different

49
00:03:02,100 --> 00:03:02,840
ways.

50
00:03:03,000 --> 00:03:07,560
If we want to use our variable as an observable we need to tell our ex-wifes swift so.

51
00:03:07,740 --> 00:03:13,420
So go ahead and type variable dot as observable.

52
00:03:13,420 --> 00:03:14,680
Do you see that.

53
00:03:14,980 --> 00:03:20,090
And you know if we look at the description of this it says it's a canonical interface for push style

54
00:03:20,090 --> 00:03:20,910
sequence.

55
00:03:21,050 --> 00:03:26,720
That's not very clear but basically what you need to know is that this is going to convert our variable

56
00:03:27,050 --> 00:03:29,560
into a variable that is observable.

57
00:03:29,730 --> 00:03:30,050
OK.

58
00:03:30,080 --> 00:03:36,190
Otherwise it's just sort of like an ordinary variable you know what I actually I don't know if it's

59
00:03:36,190 --> 00:03:42,700
possible to do this let's try to just print the variable and see what happens if we printed out OK.

60
00:03:42,710 --> 00:03:47,150
So it shows up as a type or swift variable Swift got string.

61
00:03:47,420 --> 00:03:50,940
But I wonder if we were to pull out the value of that if it would print out.

62
00:03:50,960 --> 00:03:51,830
Hello.

63
00:03:52,180 --> 00:03:52,450
Okay.

64
00:03:52,450 --> 00:03:53,240
And it does so.

65
00:03:53,330 --> 00:03:56,160
Just so you know that's new to me as well.

66
00:03:56,210 --> 00:03:59,100
You can go into a variable and pull out its value.

67
00:03:59,290 --> 00:04:00,500
That's pretty cool.

68
00:04:00,500 --> 00:04:05,370
So anyway we have now cast our variable here as an observable.

69
00:04:05,480 --> 00:04:09,650
And next we're going to go ahead and try to subscribe to pull out the value.

70
00:04:09,770 --> 00:04:10,640
So go ahead and type.

71
00:04:10,640 --> 00:04:17,870
Dot subscribe and inside of Subscribe we're using on next just like we have in our previous videos with

72
00:04:17,990 --> 00:04:24,320
other subjects and I added some curly brackets there so that we have a closure that we're using the

73
00:04:24,530 --> 00:04:25,670
next closure.

74
00:04:25,850 --> 00:04:33,080
And basically we have already passed in a value we've already called on next once and so we can go ahead

75
00:04:33,110 --> 00:04:39,830
and we can print whatever that value is by using the dollar sign 0 K that's a temporary variable that's

76
00:04:39,830 --> 00:04:44,630
going to cycle through this whole sequence called variable and print out whatever is in there.

77
00:04:44,630 --> 00:04:47,830
And as you can see Hello Prince right out to the con..

78
00:04:47,960 --> 00:04:49,050
Pretty cool.

79
00:04:49,580 --> 00:04:53,950
So since this is a variable we can't pass in New values right.

80
00:04:53,950 --> 00:04:56,920
A variable can only have one value at a time.

81
00:04:56,930 --> 00:05:06,260
So if I try to go in and type variable dot on next doesn't work if I try variable as observable on next

82
00:05:06,890 --> 00:05:07,990
it doesn't work.

83
00:05:08,060 --> 00:05:08,520
OK.

84
00:05:08,720 --> 00:05:14,270
We cannot use this in the same way that we have been using other subjects and other observables.

85
00:05:14,270 --> 00:05:16,600
Think of a variable just like an ordinary variable.

86
00:05:16,600 --> 00:05:20,740
It's sort of like you're probably going to be most comfortable with variables.

87
00:05:20,840 --> 00:05:28,220
This type of variable with the capital V from our X Swift is going to feel a lot more natural to use

88
00:05:28,550 --> 00:05:35,120
if you're a non-reactive programmer which if you're taking this course I'm assuming you are we can use

89
00:05:35,120 --> 00:05:36,740
it just like an ordinary variable.

90
00:05:36,740 --> 00:05:44,360
All we need to do is call variable value and we can pull out the value that we want Ok that is variables.

91
00:05:44,360 --> 00:05:47,630
I mean honestly I wish that there was more I could tell you about but this is it.

92
00:05:47,630 --> 00:05:53,390
Variables are really simple and are swift just like they are in ordinary swift So this is Caleb with

93
00:05:53,390 --> 00:05:57,050
those slopes dot com and in the next video you're going to have a challenge.

94
00:05:57,050 --> 00:06:01,310
I'm going to challenge you to use your knowledge of subjects to build something really cool in code.

95
00:06:01,320 --> 00:06:04,190
So head over to the next video for your challenge.
