0
1
00:00:00,450 --> 00:00:00,750
All right.
1

2
00:00:00,780 --> 00:00:05,640
So in order to use UserDefault, we have to create a brand-new object.
2

3
00:00:05,640 --> 00:00:13,080
So let's call it defaults and we'll set it to equal UserDefaults which is an interface to the user's
3

4
00:00:13,080 --> 00:00:19,640
default's database where you store key-value pairs persistently across launches of your app.
4

5
00:00:19,650 --> 00:00:21,270
That's exactly what we want.
5

6
00:00:21,330 --> 00:00:26,190
So let's hit UserDefaults and we're gonna set up a standard UserDefault.
6

7
00:00:26,190 --> 00:00:32,940
So, now that we've got that setup, we can go down to the part where we add a new item to our to-do list
7

8
00:00:33,540 --> 00:00:39,860
and we append it to our itemArray which is this thing here.
8

9
00:00:39,930 --> 00:00:48,930
Now, what we can do is we can save that updated item array to our UserDefaults and we can do that in
9

10
00:00:48,930 --> 00:00:51,250
a single line of code by writing
10

11
00:00:51,360 --> 00:00:58,050
defaults.set and we're going to use value for key which takes any object,
11

12
00:00:58,050 --> 00:01:02,000
and in this case, it's going to be an array or rather self.itemArray
12

13
00:01:02,000 --> 00:01:04,330
because, remember, we're inside a closure here.
13

14
00:01:04,500 --> 00:01:10,100
And the key is going to identify this array inside our UserDefaults.
14

15
00:01:10,110 --> 00:01:13,730
So let's call it "TodoListArray."
15

16
00:01:14,070 --> 00:01:20,850
And, of course, because we're inside a closure, we have to add "self" to our defaults as well, and see if
16

17
00:01:20,850 --> 00:01:21,780
it works.
17

18
00:01:21,780 --> 00:01:23,550
All right, let's go ahead and add a new item.
18

19
00:01:26,320 --> 00:01:29,340
Hit Add Item, shows up in our table view,
19

20
00:01:29,620 --> 00:01:37,700
But as you'll see, if I delete our app and reopen it again, it does not show up.
20

21
00:01:37,840 --> 00:01:44,140
And the reason is not because our code didn't work or that we didn't manage to save our item array into
21

22
00:01:44,140 --> 00:01:45,100
our defaults,
22

23
00:01:45,100 --> 00:01:50,790
the reason is because we haven't used our saved item to load up the TableView.
23

24
00:01:50,800 --> 00:01:56,340
It's still getting all of that data from our itemArray which does not persist.
24

25
00:01:56,350 --> 00:02:05,260
Remember? But we can actually view our data inside our UserDefaults and we can prove that the data has
25

26
00:02:05,260 --> 00:02:06,540
been saved.
26

27
00:02:06,550 --> 00:02:14,260
Now, UserDefaults get saved in a plist file, and that's why everything you put in there has to be a
27

28
00:02:14,260 --> 00:02:15,770
key-value pair.
28

29
00:02:16,080 --> 00:02:24,180
So you always need a key that you're going to use to retrieve the item, and then you add the value.
29

30
00:02:24,310 --> 00:02:25,270
It could be anything.
30

31
00:02:25,270 --> 00:02:27,990
It could be an array. It could be a dictionary, string,
31

32
00:02:28,210 --> 00:02:33,250
any data type in there. And you can grab it back using this key.
32

33
00:02:33,250 --> 00:02:41,850
So how can we find our UserDefaults file so that we can see our data? In order to do that,
33

34
00:02:41,890 --> 00:02:52,300
we need to grab the file path of our sandbox that our app runs, we need to get the ID of the simulator,
34

35
00:02:52,700 --> 00:02:58,040
and we also need the ID of the sandbox where our app lives in.
35

36
00:02:58,270 --> 00:03:02,150
Now, it sounds a little bit complicated, but it's actually quite simple.
36

37
00:03:02,200 --> 00:03:04,860
Let's go into our AppDelegate at the
37

38
00:03:04,930 --> 00:03:07,640
"didFinishLaunchingWithOptions"
38

39
00:03:08,020 --> 00:03:11,290
delegate method which we know always gets called.
39

40
00:03:11,440 --> 00:03:17,170
And here, we're going to write a little bit of code in order to printout the path for our UserDefaults
40

41
00:03:17,170 --> 00:03:17,880
file.
41

42
00:03:17,890 --> 00:03:24,550
Now, at this point, if you want to follow along with me and see this file path, make sure that you run
42

43
00:03:24,550 --> 00:03:31,690
your app inside the simulator, and not a physical device, and also that you've added a new item to your
43

44
00:03:31,690 --> 00:03:32,930
table view.
44

45
00:03:33,010 --> 00:03:36,150
So let's go ahead and print this search path.
45

46
00:03:36,190 --> 00:03:46,830
So it's NSSearchPathForDirectoriesInDomains and the directory is the documentDirectory and
46

47
00:03:46,830 --> 00:03:53,530
the domain mask is the userDomainMask and expand tilde will go for true,
47

48
00:03:53,550 --> 00:04:00,070
and then we're going to get the last item in this array, and we're going to print it as a string.
48

49
00:04:00,090 --> 00:04:02,580
Now, don't worry too much about this line.
49

50
00:04:02,610 --> 00:04:09,480
I'm just using it to print and to prove to you that our data has been saved somewhere, even though it's
50

51
00:04:09,480 --> 00:04:10,530
not showing up.
51

52
00:04:10,530 --> 00:04:17,490
So let's run our app again. And as soon as our app launches, we can see this file path here.
52

53
00:04:17,490 --> 00:04:24,360
Now, we can use that file path to go into our Finder and try and locate this plist file that stores
53

54
00:04:24,360 --> 00:04:25,170
our data.
54

55
00:04:25,170 --> 00:04:30,200
So this starts off in our root which is our Macintosh hard drive,
55

56
00:04:30,390 --> 00:04:32,910
and then we're supposed to go into users,
56

57
00:04:33,090 --> 00:04:36,150
and this is my user name on this laptop,
57

58
00:04:36,210 --> 00:04:39,860
and then we're going to go into Library, Developer,
58

59
00:04:42,930 --> 00:04:52,710
CoreSimulator, Devices,and we have to search for this 8D49.
59

60
00:04:52,720 --> 00:04:53,560
It looks like that one.
60

61
00:04:53,680 --> 00:04:56,920
So this is the device ID.
61

62
00:04:56,920 --> 00:05:04,210
So this is called a UUID, and it identifies our device uniquely, so that could be our simulated device
62

63
00:05:04,300 --> 00:05:06,610
or it could be a physical device.
63

64
00:05:06,610 --> 00:05:13,630
So this corresponds to this particular simulated instance that I've got launched here. And then I have
64

65
00:05:13,630 --> 00:05:23,610
to go in into Data, Containers, Data again, Application, and then I have to search for the Application.
65

66
00:05:23,800 --> 00:05:31,030
And that refers to the unique ID of this Todoey application that I've launched earlier on where I
66

67
00:05:31,030 --> 00:05:32,470
saved my data.
67

68
00:05:32,470 --> 00:05:39,160
So, now we're entering the sandbox, essentially, and this is going to be called E5.
68

69
00:05:39,220 --> 00:05:46,450
So let's look for something that is E5. Now a much easier way to do this would actually be sorting
69

70
00:05:46,450 --> 00:05:47,540
this or searching it,
70

71
00:05:47,620 --> 00:05:49,020
but I found it right there.
71

72
00:05:49,030 --> 00:05:50,710
So E50.
72

73
00:05:50,710 --> 00:05:54,830
This is basically our app, Todoey. Now, it would be easier if they called it Todoey,
73

74
00:05:54,880 --> 00:05:56,640
but then that wouldn't be unique.
74

75
00:05:57,100 --> 00:05:59,160
So here's our app sandbox.
75

76
00:05:59,170 --> 00:06:05,530
And once you've gotten here, you're going to look inside Library and Preferences.
76

77
00:06:05,530 --> 00:06:09,470
And now, finally, you get to your plist,
77

78
00:06:09,520 --> 00:06:14,060
and if we expand it, you can see that it says,
78

79
00:06:14,080 --> 00:06:16,390
"com.londonbrewery.Todoey/plist.
79

80
00:06:16,390 --> 00:06:23,680
Now, if we double click on it, it'll open by default inside Xcode, and you'll see that we have this key
80

81
00:06:23,860 --> 00:06:31,240
that we used earlier on where we said save it under the key "TodoListArray," TodoListArray here,
81

82
00:06:31,270 --> 00:06:39,700
that's the key, and the value is an array. So Swift is clever enough to figure out that our item, the data
82

83
00:06:39,700 --> 00:06:42,520
type was an array data type.
83

84
00:06:42,640 --> 00:06:47,530
And if we expand this, we can see that there's four items in there,
84

85
00:06:47,590 --> 00:06:51,740
and our latest item was in fact saved.
85

86
00:06:51,790 --> 00:06:54,490
So why is it not showing up in our table view?
86

87
00:06:55,120 --> 00:06:59,960
Well, it's because we haven't used it to retrieve the data.
87

88
00:07:00,070 --> 00:07:08,920
Now, all we need to do in order to retrieve our data is we can go into viewDidLoad and we can set our
88

89
00:07:08,950 --> 00:07:15,990
itemArray to equal the array inside our UserDefaults.
89

90
00:07:16,060 --> 00:07:23,120
So itemArray = defaults.array forKey,
90

91
00:07:23,120 --> 00:07:27,510
the key is, of course, "TodoListArray."
91

92
00:07:27,610 --> 00:07:30,840
Let's just double check because it's a hardcoded key.
92

93
00:07:30,910 --> 00:07:34,540
Just make sure that it's exactly spelt the same.
93

94
00:07:34,540 --> 00:07:35,980
Yeah, looks good.
94

95
00:07:36,130 --> 00:07:40,950
And we're going to cast this as an array of strings.
95

96
00:07:40,990 --> 00:07:48,400
So that's all very well and good if our TodoListArray actually exists. But if it doesn't, then our
96

97
00:07:48,460 --> 00:07:51,330
app is probably going to crash.
97

98
00:07:51,370 --> 00:07:53,030
So to prevent this,
98

99
00:07:53,230 --> 00:08:04,960
instead, we can write this as if let items = defaults.array forKey as array of strings,
99

100
00:08:04,960 --> 00:08:14,820
And if that works, then, let's say, itemArray is now equal to items. And we can get rid of that force
100

101
00:08:14,820 --> 00:08:15,510
downcast,
101

102
00:08:15,520 --> 00:08:16,750
make it an optional.
102

103
00:08:16,750 --> 00:08:20,350
So, now let's run our app and see if it works.
103

104
00:08:20,420 --> 00:08:20,820
Hey.
104

105
00:08:20,830 --> 00:08:26,750
So our last item that we saw inside the plist now exists inside our table view,
105

106
00:08:26,950 --> 00:08:35,350
and also if I add any new items, then it also shows up in our table view. And you can see it appear both
106

107
00:08:35,350 --> 00:08:40,330
in the UserDefault plist, as well as when we terminate our app and reopen it.
107

108
00:08:40,330 --> 00:08:47,320
So that was a quick intro into how we can use UserDefaults to persist data.
108

109
00:08:47,320 --> 00:08:52,900
Now, in the next lesson, we're going to talk about the UserDefault in a little bit more detail and talk about
109

110
00:08:52,900 --> 00:08:56,910
which scenarios and situations should you be using this.
110

111
00:08:56,920 --> 00:08:59,400
So all of that and more on the next lesson.
111

112
00:08:59,440 --> 00:08:59,920
See you there.
