Now, in quite a few places, you'll see these things. For example, UserDefaults, or, for example, 1
sharedURLSession. 2
These are all what are known as singletons. And what's special about singletons is that there is only 3
one copy of it that can be shared across all of your classes and objects. 4
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
code colour that's going to be set to equal "Red." 6
Now, say, later on we create some objects. We create 7
myCar = Car. 8
And we're going to set myCar's colour property to blue. 9
So, say, my favorite color, right? 10
Now, let's say we create another object of car and we call it yourCar. 11
Now, if we tried to print yourCar's color property, can you guess what the value of it will be based 12
on our code? 13
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
myCar and it's getting its color from the defaults which is the color "Red." 15
Now, let's try and do the same thing, but this time, we're going to create a singleton. So let's create 16
a new class and I'm going to call it exactly the same. 17
I'm going to call it Car and I'm going to have a property called colour which equals "Red." Now, the special 18
thing inside this class is that I'm going to create a singleton. 19
I'm going to use the "static let" keywords and I'm going to call my singleton singletonCar and this 20
is going to be set to equal a new object initialized from its own class, 21
the Car class. Now, instead of creating a new object of car in this file, I'm going to create a new constant 22
that holds a reference to our singleton. 23
So let's use the same name, 24
let myCar = Car.singletonCar. 25
And I say myCar.colour = "Blue." 26
Now, later on, let's create another reference to our singleton. 27
Let's call that yourCar and we say 28
Car.singletonCar. And we try to print the value of your car's colour. 29
Now, in this case, we get blue because it doesn't matter how many constants or variables, or different 30
ways that we point towards this singletonCar, 31
it's always going to be the same copy. 32
So I can use it in different objects and different classes, so let's say, class A. 33
So where when we initialized class A, we say Car.singletonCar.colour to "Brown," and class B-- 34
class B, we print Car.singletonCar.colour, and we initialize a 35
and b as new objects. You can see that even though we're affecting the color of the singletonCar in 36
completely different classes, it's still tapping into the same thing in the entirety of this file. Every 37
single time, we're trying to change or rather get and set our singletonCar, 38
it's always been the same object. 39
And when we think back to our UserDefaults, it's exactly the same. 40
We're using that UserDefaults.standard as a singleton to this UserDefault. 41
So you can see that this line and this line look structurally the same. And that's because inside this 42
class code UserDefaults, 43
there is a singleton called standard, and this points towards the same list every single time we tap 44
into the UserDefaults. 45
So we're always retrieving and saving to the same file and it stays the same across all of our classes 46
and all of our objects. 47
So singletons exist in other programming languages aside from Swift. 48
So if you've programmed in other languages, this concept might not be new to you at all. 49
But if this is the first time you've ever come across the singleton or how it works or what it's all 50
about, it might be worth having a read of the Swift Documentation which I'm going to link in the resources 51
for this lesson. 52
And if you want to learn more about UserDefaults in terms of all the different default values that 53
you can store and you can retrieve, then have a look at the documentation which is really good on this.