0
1
00:00:01,320 --> 00:00:12,600
Now, in quite a few places, you'll see these things. For example, UserDefaults, or, for example,
1

2
00:00:12,600 --> 00:00:15,140
sharedURLSession.
2

3
00:00:15,240 --> 00:00:22,260
These are all what are known as singletons. And what's special about singletons is that there is only
3

4
00:00:22,260 --> 00:00:28,080
one copy of it that can be shared across all of your classes and objects.
4

5
00:00:28,090 --> 00:00:34,620
Now, let's say, we create a new class that's called Car, and by default, I'm going to give it a property
5

6
00:00:34,650 --> 00:00:39,390
code colour that's going to be set to equal "Red."
6

7
00:00:39,600 --> 00:00:43,590
Now, say, later on we create some objects. We create
7

8
00:00:43,620 --> 00:00:47,640
myCar = Car.
8

9
00:00:48,250 --> 00:00:53,020
And we're going to set myCar's colour property to blue.
9

10
00:00:53,050 --> 00:00:55,850
So, say, my favorite color, right?
10

11
00:00:55,870 --> 00:01:01,220
Now, let's say we create another object of car and we call it yourCar.
11

12
00:01:01,300 --> 00:01:10,360
Now, if we tried to print yourCar's color property, can you guess what the value of it will be based
12

13
00:01:10,360 --> 00:01:11,370
on our code?
13

14
00:01:12,380 --> 00:01:21,230
So the value of it is "Red" and that's because it's a new object that has nothing to do with this object
14

15
00:01:21,230 --> 00:01:27,780
myCar and it's getting its color from the defaults which is the color "Red."
15

16
00:01:27,780 --> 00:01:33,410
Now, let's try and do the same thing, but this time, we're going to create a singleton. So let's create
16

17
00:01:33,410 --> 00:01:36,770
a new class and I'm going to call it exactly the same.
17

18
00:01:36,770 --> 00:01:45,770
I'm going to call it Car and I'm going to have a property called colour which equals "Red." Now, the special
18

19
00:01:45,770 --> 00:01:49,720
thing inside this class is that I'm going to create a singleton.
19

20
00:01:49,910 --> 00:01:57,620
I'm going to use the "static let" keywords and I'm going to call my singleton singletonCar and this
20

21
00:01:57,620 --> 00:02:04,440
is going to be set to equal a new object initialized from its own class,
21

22
00:02:04,490 --> 00:02:12,230
the Car class. Now, instead of creating a new object of car in this file, I'm going to create a new constant
22

23
00:02:12,630 --> 00:02:15,590
that holds a reference to our singleton.
23

24
00:02:15,890 --> 00:02:18,400
So let's use the same name,
24

25
00:02:18,410 --> 00:02:24,980
let myCar =  Car.singletonCar.
25

26
00:02:25,190 --> 00:02:31,080
And I say myCar.colour = "Blue."
26

27
00:02:31,790 --> 00:02:36,890
Now, later on, let's create another reference to our singleton.
27

28
00:02:36,890 --> 00:02:41,380
Let's call that yourCar and we say
28

29
00:02:41,420 --> 00:02:53,070
Car.singletonCar. And we try to print the value of your car's colour.
29

30
00:02:53,120 --> 00:03:00,650
Now, in this case, we get blue because it doesn't matter how many constants or variables, or different
30

31
00:03:00,650 --> 00:03:04,680
ways that we point towards this singletonCar,
31

32
00:03:04,910 --> 00:03:08,730
it's always going to be the same copy.
32

33
00:03:08,780 --> 00:03:14,640
So I can use it in different objects and different classes, so let's say, class A.
33

34
00:03:14,660 --> 00:03:26,150
So where when we initialized class A, we say Car.singletonCar.colour to "Brown," and class B--
34

35
00:03:29,340 --> 00:03:40,190
class B, we print Car.singletonCar.colour, and we initialize a
35

36
00:03:42,810 --> 00:03:52,110
and b as new objects. You can see that even though we're affecting the color of the singletonCar in
36

37
00:03:52,320 --> 00:03:59,580
completely different classes, it's still tapping into the same thing in the entirety of this file. Every
37

38
00:03:59,580 --> 00:04:05,090
single time, we're trying to change or rather get and set our singletonCar,
38

39
00:04:05,160 --> 00:04:08,010
it's always been the same object.
39

40
00:04:08,100 --> 00:04:12,500
And when we think back to our UserDefaults, it's exactly the same.
40

41
00:04:12,540 --> 00:04:20,220
We're using that UserDefaults.standard as a singleton to this UserDefault.
41

42
00:04:20,580 --> 00:04:28,530
So you can see that this line and this line look structurally the same. And that's because inside this
42

43
00:04:28,530 --> 00:04:30,110
class code UserDefaults,
43

44
00:04:30,120 --> 00:04:36,900
there is a singleton called standard, and this points towards the same list every single time we tap
44

45
00:04:36,900 --> 00:04:38,490
into the UserDefaults.
45

46
00:04:38,490 --> 00:04:46,200
So we're always retrieving and saving to the same file and it stays the same across all of our classes
46

47
00:04:46,350 --> 00:04:47,930
and all of our objects.
47

48
00:04:47,940 --> 00:04:52,800
So singletons exist in other programming languages aside from Swift.
48

49
00:04:52,800 --> 00:04:58,300
So if you've programmed in other languages, this concept might not be new to you at all.
49

50
00:04:58,290 --> 00:05:03,990
But if this is the first time you've ever come across the singleton or how it works or what it's all
50

51
00:05:03,990 --> 00:05:10,770
about, it might be worth having a read of the Swift Documentation which I'm going to link in the resources
51

52
00:05:10,890 --> 00:05:12,680
for this lesson.
52

53
00:05:12,930 --> 00:05:19,410
And if you want to learn more about UserDefaults in terms of all the different default values that
53

54
00:05:19,410 --> 00:05:25,560
you can store and you can retrieve, then have a look at the documentation which is really good on this.
