0
1
00:00:00,330 --> 00:00:06,420
So, now that we've confirmed that our refactoring works as we expected, the next step is to reintegrate
1

2
00:00:06,450 --> 00:00:10,080
our app with UserDefaults so that we can try and persist
2

3
00:00:10,090 --> 00:00:12,900
our item objects using UserDefaults.
3

4
00:00:12,900 --> 00:00:20,580
So, now let's delete this line of code where we were using to debug previously, and let's reactivate our
4

5
00:00:20,580 --> 00:00:25,800
defaults to try and retrieve our items out of our itemArray.
5

6
00:00:26,340 --> 00:00:31,620
So before we do that, we have to make a few fixes, of course, because we're no longer retrieving an array
6

7
00:00:31,620 --> 00:00:35,340
of strings, but we're retrieving an array of items.
7

8
00:00:35,400 --> 00:00:40,450
Now, there's nothing to change here when we're adding items because, of course, self.default.set
8

9
00:00:40,460 --> 00:00:43,200
can take any object, right?
9

10
00:00:43,350 --> 00:00:45,060
Or at least theoretically.
10

11
00:00:45,090 --> 00:00:52,830
So let's see what happens when we try to run our app. Now, I'm ready hinting at what's going to come.
11

12
00:00:52,990 --> 00:01:01,440
But theoretically, at this point, we are creating a newItem using our class Item, we're setting its title
12

13
00:01:01,480 --> 00:01:02,220
property.
13

14
00:01:02,380 --> 00:01:12,520
The done property is set to false by default here. And then we're appending the item to itemArray, and
14

15
00:01:12,520 --> 00:01:19,260
then we're setting into our UserDefaults using the key "TodoListArray." And then when our app loads up,
15

16
00:01:19,270 --> 00:01:27,070
we use our UserDefaults to pull out an array. And if you remember, user defaults.array can be an
16

17
00:01:27,070 --> 00:01:30,710
array of any type, at least theoretically.
17

18
00:01:30,880 --> 00:01:37,770
And we're using the same key as we saved it and we're trying to pull it out as an array of items.
18

19
00:01:37,840 --> 00:01:43,970
So if that succeeds, then we set our itemArray to equal items.
19

20
00:01:44,080 --> 00:01:53,440
Let's go ahead and give our app a run and see what happens. So that's all working fine because we're
20

21
00:01:53,440 --> 00:01:59,660
initializing new items inside viewDidLoad and we're appending them to this itemArray.
21

22
00:01:59,830 --> 00:02:09,910
Now, the part that's interesting, because when we first press the add button and add a new item.
22

23
00:02:09,910 --> 00:02:16,100
So let's press the add button and let's add a new item.
23

24
00:02:16,180 --> 00:02:20,060
Now, once I hit add item, something dramatic happens
24

25
00:02:20,110 --> 00:02:24,690
and, of course, you can see we've got signals SIGABRT which doesn't say anything at all.
25

26
00:02:24,850 --> 00:02:27,160
But by now you should have seen it often enough
26

27
00:02:27,160 --> 00:02:35,350
if you're coding along because this means that our app has crashed. And if we scroll to the top of our
27

28
00:02:35,410 --> 00:02:42,200
debug console, as always, we'll find something that's trying to tell us why our app crashed.
28

29
00:02:42,460 --> 00:02:53,170
And the reason that it's telling us is attempting to set a non-property-list object using UserDefaults.
29

30
00:02:53,170 --> 00:02:54,970
So what does that mean?
30

31
00:02:55,900 --> 00:03:00,400
Well, did you remember how I said that UserDefaults is a property list?
31

32
00:03:00,460 --> 00:03:03,840
It has a whole bunch of key-value pairs.
32

33
00:03:03,910 --> 00:03:11,110
So, for example, you could have a key which is "TodoListArray" and the value is an array of strings.
33

34
00:03:11,200 --> 00:03:19,000
But in our case, we're trying to save an array of custom Item objects.
34

35
00:03:19,000 --> 00:03:24,520
Remember, our itemArray is an array of our own Item objects.
35

36
00:03:24,520 --> 00:03:30,880
and we're trying to save that into UserDefaults which it's rejecting, and it probably should reject,
36

37
00:03:30,910 --> 00:03:36,850
because by the time that you're trying to save an array of your custom objects, then you're probably
37

38
00:03:36,850 --> 00:03:38,800
misusing UserDefaults,
38

39
00:03:38,800 --> 00:03:45,040
so this is the extent where we can get to with the UserDefaults. And we have to start considering a
39

40
00:03:45,040 --> 00:03:51,700
slightly better solution than using UserDefaults to persist data which if you remember is only really
40

41
00:03:51,700 --> 00:03:58,330
intended for small pieces of data, and it's massively inefficient because it has to load up the entire
41

42
00:03:58,330 --> 00:04:03,580
plist before you can use or read any of the properties inside it.
42

43
00:04:03,580 --> 00:04:07,570
So we have to abandon ship and stop using UserDefaults.
43

44
00:04:07,750 --> 00:04:14,010
So I believe that it's always better to show rather than tell. And I can, of course, warn you don't use
44

45
00:04:14,000 --> 00:04:14,820
UserDefaults.
45

46
00:04:14,860 --> 00:04:19,540
It's not going to be very efficient and it's not going to work in lots of cases.
46

47
00:04:19,600 --> 00:04:24,320
But I think it's better to show you when we actually try to implement it and reach its limits.
47

48
00:04:24,340 --> 00:04:30,930
So remember, don't use UserDefaults for anything other than, basically, that right UserDefaults,
48

49
00:04:30,940 --> 00:04:39,220
so volume on, volume off, volume high, volume low. these kind of small tidbits. For bigger pieces of data
49

50
00:04:39,310 --> 00:04:44,350
and for saving data into databases, we've got better tools for them.
50

51
00:04:44,560 --> 00:04:50,220
So in the next lesson, we're going to be learning about one of those tools which is NSCoder.
51

52
00:04:50,350 --> 00:04:51,190
So I'll see you there.
