1
00:00:03,750 --> 00:00:05,000
Hey what's up.

2
00:00:05,270 --> 00:00:07,950
Jack Davis here with Defoe's dot com.

3
00:00:08,330 --> 00:00:14,090
And tonight we're here to talk about a very popular subject over the last year or so and that subject

4
00:00:14,090 --> 00:00:17,410
is protocol oriented programming.

5
00:00:17,450 --> 00:00:25,920
There was a talk given w DC in 2015 that generated a huge amount of buzz on this subject.

6
00:00:26,000 --> 00:00:32,810
It continues to generate buzz to this day sometimes is referred to as the crusty talk.

7
00:00:32,810 --> 00:00:37,470
I highly recommend that you watch this session if you haven't already.

8
00:00:37,640 --> 00:00:44,810
What edition of protocol extensions protocol oriented programming has become even bigger.

9
00:00:45,140 --> 00:00:50,990
And in this introduction we're going to cover a few different topics such as swift tops the differences

10
00:00:50,990 --> 00:00:57,890
between object oriented programming and protocol oriented programming protocols themselves protocol

11
00:00:57,890 --> 00:01:00,460
extensions and generics.

12
00:01:00,480 --> 00:01:07,010
Then to wrap things up we'll take a brief look at practical real world examples of how protocol oriented

13
00:01:07,010 --> 00:01:10,650
programming can be used in your own projects.

14
00:01:10,820 --> 00:01:16,910
Now this is just an introduction and this is a topic that can get very complex.

15
00:01:16,910 --> 00:01:22,880
I hope you're as interested as I am and digging in continue learning new ways that protocol oriented

16
00:01:22,880 --> 00:01:26,250
programming can help you to write better code.

17
00:01:26,300 --> 00:01:29,180
There is a ton of information out there about it.

18
00:01:29,180 --> 00:01:32,370
With that said let's move on and get started.

19
00:01:32,450 --> 00:01:39,120
First we'll quickly cover the swift type system.

20
00:01:39,390 --> 00:01:44,400
So for the first part of this segment we're going to talk about swift types and I'm going to pull up

21
00:01:44,400 --> 00:01:48,320
a whiteboard at appears so I can sketch those out for you.

22
00:01:48,360 --> 00:01:50,590
This is just going to be a quick run through.

23
00:01:50,590 --> 00:01:52,330
Hopefully you're already familiar with them.

24
00:01:52,500 --> 00:01:57,360
Some of the things you may not be familiar with but we're going to go ahead and lay those out just because

25
00:01:57,360 --> 00:01:59,240
they may come up later on.

26
00:01:59,250 --> 00:02:01,850
So let's pull up a white board here.

27
00:02:03,380 --> 00:02:05,070
All right.

28
00:02:05,430 --> 00:02:08,220
So we've got swift tops

29
00:02:13,800 --> 00:02:17,800
and basically swift taps are broken into two categories.

30
00:02:17,860 --> 00:02:19,980
We have nine tops

31
00:02:22,660 --> 00:02:30,220
and we have compound tops and we're not going to talk about compound tops for a whole lot but I'm going

32
00:02:30,220 --> 00:02:34,200
to go ahead and lay them out here just so you know how it works.

33
00:02:34,270 --> 00:02:36,320
Compound types.

34
00:02:37,030 --> 00:02:42,870
We have a group of top set or functions.

35
00:02:43,000 --> 00:02:47,800
And the reason I say it's a group of tops is because it includes functions themselves

36
00:02:52,000 --> 00:02:56,030
as well as closures.

37
00:02:59,080 --> 00:03:01,730
And then also in compound types we have tuples

38
00:03:04,690 --> 00:03:08,640
and I don't know if you're familiar with tuples or not you may have seen them in code before.

39
00:03:08,820 --> 00:03:14,940
It's basically where you have two two or more types enclosed in parentheses.

40
00:03:15,020 --> 00:03:19,840
You can pass those around in your code if you don't know what tuples are highly recommended that you

41
00:03:19,840 --> 00:03:20,430
looked at.

42
00:03:20,450 --> 00:03:24,520
Those are very handy in certain instances.

43
00:03:25,300 --> 00:03:31,150
So let's look at nine types and that's mainly what we're going to focus on here and under nine types

44
00:03:31,150 --> 00:03:34,480
we have we have primitives

45
00:03:40,530 --> 00:03:49,730
and primitives are things like integers doubles strings characters things like that in most languages.

46
00:03:49,740 --> 00:03:56,850
Those are all just built in to the language in swift they're handled a little bit differently they're

47
00:03:56,880 --> 00:04:00,390
actually implemented in the standard library structures.

48
00:04:00,660 --> 00:04:05,450
Most of them anyway and will will probably take a look at that a little bit later.

49
00:04:05,670 --> 00:04:11,670
I just want you to understand that in swift the way that they create those is they take structures and

50
00:04:11,670 --> 00:04:18,010
they put extensions in there to give them functionality that's built into other languages.

51
00:04:18,660 --> 00:04:21,970
So we've got primitives.

52
00:04:23,340 --> 00:04:35,020
So we've got so primitive as a name type but it's also a value tot and undervalue types besides primitives

53
00:04:35,040 --> 00:04:36,640
we also have structures

54
00:04:41,430 --> 00:04:46,730
and gnomes or enumerations.

55
00:04:46,870 --> 00:04:47,640
All right.

56
00:04:47,940 --> 00:04:52,270
And then also under the name types we have reference types

57
00:04:58,750 --> 00:05:04,230
and it's kind of important to understand the difference because if you don't it could cause problems

58
00:05:04,660 --> 00:05:09,280
and under reference types of classes.

59
00:05:10,990 --> 00:05:16,350
And with this being a little bit more of an advanced topic hopefully you are familiar with Object-Oriented

60
00:05:16,360 --> 00:05:23,480
Programming and classes and a lot of times we just kind of default to using classes.

61
00:05:23,890 --> 00:05:29,930
But we're going to talk about object oriented programming obviously.

62
00:05:30,160 --> 00:05:35,200
And the fact that sometimes classes aren't always the best answer.

63
00:05:35,470 --> 00:05:39,740
Sometimes it's better to use a structure or it as a value type.

64
00:05:40,300 --> 00:05:45,290
And basically what that means is value types are passed as copies.

65
00:05:45,610 --> 00:05:51,600
So when you say the eraser.

66
00:05:52,300 --> 00:05:54,780
So let's go ahead and clean this off here.

67
00:05:56,940 --> 00:06:02,290
So we're going to talk about the way objects are created.

68
00:06:02,430 --> 00:06:08,100
So if we have a reference type if you remember that would be a class

69
00:06:11,190 --> 00:06:15,660
and we've got a value type over here.

70
00:06:15,660 --> 00:06:18,600
And for this case we're just going to say a structure.

71
00:06:19,160 --> 00:06:21,420
So over here we've got a class.

72
00:06:21,420 --> 00:06:25,650
And over here we've got a struct.

73
00:06:25,740 --> 00:06:36,180
Now if you create an object out of each of these things so say we do a variable and we'll say car one

74
00:06:37,560 --> 00:06:39,480
equal to an instance of a car

75
00:06:44,090 --> 00:06:44,760
OK.

76
00:06:44,790 --> 00:06:55,920
And then over here say we implemented car as a struct instead of a class so we can save or we'll save

77
00:06:55,920 --> 00:06:59,270
our car one equals car again.

78
00:07:00,480 --> 00:07:08,610
Now obviously on the left side we've implemented a car as a class on the ride as a struct and then if

79
00:07:08,610 --> 00:07:19,630
we come in and we save our car two equals car 1 and we do the same thing on the other side

80
00:07:25,670 --> 00:07:31,470
we have two different things here on the class side or the reference side.

81
00:07:31,470 --> 00:07:38,700
Both of these instances point to the same object.

82
00:07:38,910 --> 00:07:50,490
So if we did something like car to dark color and the default color was blue if we do something like

83
00:07:50,500 --> 00:08:00,450
car to color and we say equals red since that's a reference and they both point to the same thing car

84
00:08:00,560 --> 00:08:01,050
one

85
00:08:04,920 --> 00:08:11,000
color is also going to be red.

86
00:08:11,110 --> 00:08:15,390
Now the end on the stroke side which is of value it's passed by value.

87
00:08:15,760 --> 00:08:23,830
So when you make this assignment in this line right here what that does it doesn't copy a link or reference

88
00:08:23,830 --> 00:08:28,950
to a car too it makes a copy of the actual data and passes it as a copy.

89
00:08:29,200 --> 00:08:31,020
So if we do the same thing over here

90
00:08:36,000 --> 00:08:47,110
when we say car to talk color equals red car one color is still going to be equal to blue.

91
00:08:48,680 --> 00:08:51,410
And it's kind of an important thing to understand.

92
00:08:51,990 --> 00:08:57,640
In most cases we keep up with the flow in our programs and we know exactly what's going on.

93
00:08:57,750 --> 00:09:04,410
And if you get in a very complex hierarchy and situation you've really got to be aware that if you set

94
00:09:04,410 --> 00:09:09,510
something in one part of your code that you don't have unintended consequences and another part of your

95
00:09:09,510 --> 00:09:10,170
code.

96
00:09:10,470 --> 00:09:15,780
So that's kind of the point of this here's just demonstrating that classes are passed by reference.

97
00:09:16,020 --> 00:09:23,400
So if you create if you set something to a copy it's not a copy it's a reference they both point to

98
00:09:23,400 --> 00:09:26,530
the same thing whereas a value type.

99
00:09:26,760 --> 00:09:32,970
It passes it as a copy and there's places where reference types are better suited for the task.

100
00:09:32,970 --> 00:09:37,670
And there's other places where value types are suited for better suited for the task.

101
00:09:37,680 --> 00:09:46,650
Apple always recommends going with a value type wherever possible to quote parts of the WWT see 20:15

102
00:09:46,650 --> 00:09:49,240
video I spoke of earlier.

103
00:09:49,380 --> 00:09:56,400
Strokes are preferable if they're relatively small and copyable copying is way safer than having multiple

104
00:09:56,400 --> 00:09:58,550
references to the same instance.

105
00:09:58,770 --> 00:10:04,550
Especially important when passing around a variable to many classes in a multi-threaded environment.

106
00:10:04,650 --> 00:10:09,720
If you can always send a copy of your variable to other places you never have to worry about that other

107
00:10:09,720 --> 00:10:15,870
place changing the value of your variable underneath the strokes there is no need to worry about memory

108
00:10:15,870 --> 00:10:20,940
leaks or multiple threads Reisen to access a single instance of a variable.

109
00:10:20,940 --> 00:10:26,440
Classes can also become bloated because of class can only inherit from a single superclass.

110
00:10:26,640 --> 00:10:33,030
That encourages us to create huge superclasses with many different abilities that are only loosely related.

111
00:10:33,340 --> 00:10:39,660
Use protocols and protocol extensions allows us to eliminate the need for classes to achieve that sort

112
00:10:39,660 --> 00:10:41,950
of behavior.

113
00:10:41,970 --> 00:10:44,800
That's the official word from Apple on the subject.

114
00:10:45,030 --> 00:10:51,410
Struction gnomes are preferred and classes should be kind of like a fallback whenever they make sense.

115
00:10:51,420 --> 00:10:56,970
There are some instances where classes are going to be the only option you have.

116
00:10:56,970 --> 00:11:01,210
Anyway I hope you enjoy this intro and I'll look forward to see you later.

117
00:11:01,650 --> 00:11:01,840
Later.
