Now, through your journey as an iOS developer, inevitably, you will come across UserDefaults 1
and you'll probably use it in quite a few of your projects because it's really simple to use, very flexible, and 2
doesn't take very many lines of code. 3
Now, let's have a look at some of the things that you can do with UserDefaults. 4
Let's establish a default and we'll call it-- 5
Now, remember, when you create property names or variable names, you can't call it certain words like default 6
or class, and you can tell because it highlights it in a different color 7
in this sort of pinkish color that tells you that this is a keyword that's reserved for certain uses. 8
So let's create a new UserDefault called defaults and we'll set it to equal UserDefaults.standard. 9
And let's see what are some of the things that we can set into our UserDefaults. So we can write something 10
like set URL forKey, set value, which is type optional any key, 11
so we can use optionals. We can set Booleans, doubles, floats, integers. 12
So one of the scenarios that you might find yourself using UserDefault is saving user preferences inside 13
your app. 14
So, for example, if you had an app where you allow the user to set the music volume, you might say something 15
like set 0.24 forKey volume, and that means that at a later date, you can say, 16
let volume = defaults.float forKey volume. 17
Then you can use that persisted data for volume to always set your games or your app's volume to 18
0.24 which is what the user prefers. 19
Now, aside from floats, you can also use set on Booleans. 20
So, for example, we'll set true for the key "MusicOn, so that every time they open up your game, you always 21
have the music on, the music off depending on their preference. 22
And there's whole bunch of other ones I'm just going to quickly run through. 23
So you can, obviously, set strings, right? 24
So, for example, you might have "PlayerName." 25
You can also track user behavior by using defaults like set, use any, we use the current date by just 26
tapping into a new object of date. 27
And the key is going to be "AppLastOpenedByUser." And you can see that when you try to retrieve this, 28
let appLastOpen = defaults.object. 29
Now, we're using object because we're not trying to grab a float and "in" to a string, 30
we're trying to grab something that was saved using optional "Any." 31
So we should try and get back using the method that grabs us an optional of any data type. 32
So let's hit object forKey and I'm just going to paste that incredibly long key into here. 33
And now, you can see that appLastOpened is set to equal the time when this default was set. 34
And, of course, every single time I run the app, it's going to change that time just by a little bit. 35
Cool. So we've seen some pretty standard data types being saved. 36
Now, if you want to get really fancy with your defaults, you can also save a collections like 37
let array = [1, 2, 3] and we can say defaults set value of optional Any to array, and forKey, 38
let's call it "myArray." 39
And there's a special method that allows you to get that array back by using defaults.array 40
forKey string, and it's going to be "myArray." 41
Now, you can either let Swift infer what's the data type of the object contained inside the array, because 42
if you option-click on this array here, you can see that when we use defaults to retrieve an array 43
out of UseDefaults. The return type is actually an optional array, 44
and it doesn't know what is the data type that's contained inside the array. 45
So if you want to be more explicit, you can downcast the array into an array of integers. And we can do 46
that safely because we saved it here and we should know what the data type is that went into that default 47
for the key "myArray." 48
And similarly, you can do the same thing for dictionaries. 49
And let's just say let dictionary = 50
[name": "Angela"] and we'll say default.set. 51
And, of course, we're using "Any" again, and we're going to put dictionary in there and 52
we'll call it "myDictionary." 53
And then we can retrieve it over here by using defaults.dictionary 54
and we'll put the key back in. 55
Now the spelling of your keys matter a great deal. 56
And if you get the case wrong or if you spell it even slightly wrong, it's probably going to crash your 57
program. 58
So it's usually a good idea to create a constant at the top when you're trying to access these things 59
like, for example, dictionaryKey, and we'll set it to equal "myDictionary." 60
So that way, instead of putting "myDictionary" as a string in all of these places, you can simply use your 61
dictionary key in all the places where you need to access it. 62
And this way if you do need to change your key, then you can do it in one place. 63
And also if you do have any errors, it's much easier to trace and debug. 64
All right. 65
So that's a little bit of an overview of UserDefault 66
and what you can do with them. As you can see, they're really flexible, really adaptable, and that's one 67
of the reasons why they're prone to abuse. 68
Now, you should only really be using UserDefaults to persist small bits of data, 69
anything that's a few kilobytes, like a volume here and there, or a Boolean, or maybe the player names. 70
The danger is once you start getting into saving arrays or dictionaries, that the data starts getting 71
really, really big, really unwieldy and unmanageable. 72
Now, the reason why we say that is because UserDefaults is not a database and it should not be used 73
as a database. As we saw earlier on, all of these key-value pairs get saved into a plist and that entire 74
plist of your UserDefault has to be loaded up synchronously. So, all of it gets loaded up before you can 75
access any of the values contained inside the keys. 76
So that means that if you're looking for something really small like the volume that was saved which 77
is going to be just 0.24 which is a very small piece of data. Even to grab that small piece 78
of data your iPhone or your simulator or whatever device you're running your app on has to load up the 79
entire UserDefaults plist. 80
So everything that you store in it has to be loaded up. 81
And that means that it can be very time consuming if you start saving large bits of data in there. And certainly 82
across the Internet, you'll come across various implementations on StackOverflow or GitHub where people 83
are basically trying to use UserDefaults as a database. But just keep in mind, it's not a database and 84
it shouldn't be used as one because it's going to have significant impact on the efficiency and the 85
speed of your app. 86
Now, one of the things that people always find really mysterious about UserDefaults is, how is it possible 87
that I can simply use UserDefault across all classes and objects and I still get back the same item. 88
And the reason for this is because it's quite special. 89
It's what's known as a singleton. And in the next lesson, I want to show you what singletons are and we'll 90
go to some of the way towards understanding how UserDefault actually works. 91
So I'll see you there.