0 1 00:00:00,740 --> 00:00:01,250 All right. 1 2 00:00:01,310 --> 00:00:07,700 So I'm putting my trust in you that you've given it a really, really good try and that you're here because 2 3 00:00:07,700 --> 00:00:11,310 you have some bugs or you just want to see how I might choose to do it. 3 4 00:00:11,330 --> 00:00:14,140 Now, in our challenges, I like to say that there's no correct way, 4 5 00:00:14,180 --> 00:00:16,100 there's only different paths. 5 6 00:00:16,100 --> 00:00:21,410 And as long as it leads you to the result that we wanted, then you have succeeded in the challenge. 6 7 00:00:21,410 --> 00:00:24,070 So let's have a look at how we might do this. 7 8 00:00:24,200 --> 00:00:25,990 Let's think about it logically. 8 9 00:00:26,120 --> 00:00:35,000 What we need to happen is that when we create a new category, it needs to be associated with a new piece 9 10 00:00:35,000 --> 00:00:43,030 of data, not just a name or a list of items, but it now needs a color. 10 11 00:00:43,040 --> 00:00:53,750 So in our category class, we're going to have to need to add a new property that is called to say colour 11 12 00:00:54,210 --> 00:01:02,430 and this is going to be set as a string data type because we're going to save a hex string into this 12 13 00:01:02,430 --> 00:01:03,780 property. 13 14 00:01:03,780 --> 00:01:10,170 Now, let's head back over to our CategoryViewController and think about where is the first time point 14 15 00:01:10,290 --> 00:01:12,620 where we create our categories. 15 16 00:01:12,810 --> 00:01:19,920 Well, that's, of course, inside our addButtonPressed method, and we need to be in here because when we 16 17 00:01:19,920 --> 00:01:27,630 create a new category, we give it values for all of its properties including the category's name. 17 18 00:01:27,930 --> 00:01:34,520 And now, we'll also need to give it a colour when we create it. 18 19 00:01:34,740 --> 00:01:44,190 So we're going to set this to equal UIColor.randomFlat. hexValue. 19 20 00:01:44,610 --> 00:01:50,490 And this is now a string, as you can see over here. 20 21 00:01:50,850 --> 00:02:01,140 So that string gets saved into that property colour and gets persisted when we call save to our newCategory. 21 22 00:02:01,140 --> 00:02:04,660 Now, once it's been saved, then we'll need to retrieve it, 22 23 00:02:04,680 --> 00:02:05,170 right? 23 24 00:02:05,400 --> 00:02:14,820 So when we retrieve--so we retrieve our data when we call loadCategories inside viewDidLoad. 24 25 00:02:14,820 --> 00:02:23,160 And loadCategories pulls out all of the objects that are of the type Category, and it gets put into this array 25 26 00:02:23,220 --> 00:02:26,300 or result's container called categories. 26 27 00:02:26,610 --> 00:02:30,020 Now, when do we need to use that information? 27 28 00:02:30,210 --> 00:02:38,910 Well, we use it here to change the textLabel of the cell and we're also going to use it here to change 28 29 00:02:38,910 --> 00:02:41,640 the color of the cell. 29 30 00:02:41,640 --> 00:02:48,270 So we're going to set it to equal a UIColor using a hexString. And this placeholder is telling 30 31 00:02:48,270 --> 00:02:50,760 us that we need to put a String inside here. 31 32 00:02:50,850 --> 00:02:52,890 So let's go ahead and do that. 32 33 00:02:52,890 --> 00:02:54,560 So what is our string going to be? 33 34 00:02:54,600 --> 00:03:06,110 Well, it's going to be the categories at indexPath.row.colour. Now, the first issue we get is that 34 35 00:03:06,110 --> 00:03:09,030 categories is an optional, 35 36 00:03:09,200 --> 00:03:15,920 so we would have to use optional chaining in order to make sure that we don't try to use a nil value. 36 37 00:03:15,920 --> 00:03:23,990 So if categories is not nil, then we're going to grab the category at indexPath.row, and then we're 37 38 00:03:23,990 --> 00:03:26,340 going to tap into the colour property. 38 39 00:03:26,570 --> 00:03:29,840 Now, colour may or may not have a value. 39 40 00:03:30,110 --> 00:03:34,890 So if it doesn't have a value, then we're going to do the same thing as we did up here. 40 41 00:03:35,030 --> 00:03:40,600 We're going to say if there is no value, then here is a default value. 41 42 00:03:40,850 --> 00:03:46,050 So the default value that we're going to give as the hex code is we're going to go into 42 43 00:03:46,050 --> 00:03:52,700 our Main.storyboard and we're going to find that nice blue color that we've got as the title, and we're going to look 43 44 00:03:52,700 --> 00:03:57,040 at that colors hex color inside the RGB Sliders 44 45 00:03:57,170 --> 00:04:04,430 under this particular tab. So we're going to copy that code over and we're going to paste it inside here, 45 46 00:04:04,850 --> 00:04:08,790 so that we can use it as the default hex color. 46 47 00:04:08,810 --> 00:04:14,600 So if your category doesn't have a color yet, then we're going to use a default value for the color. 47 48 00:04:14,750 --> 00:04:24,550 So you can shorten these two lines of code if you wish by saying if let category = categories? 48 49 00:04:25,340 --> 00:04:29,760 optional chain to indexPath.row. 49 50 00:04:29,990 --> 00:04:38,240 And if that is not nil, then we're going to call these two lines of code. And instead of using categories 50 51 00:04:38,780 --> 00:04:46,520 at indexPath.row, we're simply going to use our new constant called category, and we can replace 51 52 00:04:46,520 --> 00:04:48,290 it in those two places. 52 53 00:04:48,290 --> 00:04:53,360 All right, so let's give it a go and see if we manage to solve that challenge. 53 54 00:04:53,360 --> 00:04:58,650 So the first thing that will happen when you run your app is your app is going to crash. 54 55 00:04:58,850 --> 00:05:04,870 And that's because if we open up our Realm Browser, you'll remember that category already has three items. 55 56 00:05:04,880 --> 00:05:11,660 Now, these three items don't have the third property color yet, but color is going to be used inside our 56 57 00:05:11,750 --> 00:05:12,280 app. 57 58 00:05:12,320 --> 00:05:16,170 So when this mismatch happens, you get this particular error: 58 59 00:05:16,310 --> 00:05:24,690 "Error Domain=io.realm Code=10. Migration is required because Category.colour has been added." 59 60 00:05:24,950 --> 00:05:37,380 So, what we have to do, if you remember, is you will have to hold your app, delete it, and rerun your app. 60 61 00:05:37,540 --> 00:05:41,670 And that wipes all of your data, so that everything is consistent. 61 62 00:05:41,860 --> 00:05:42,250 Okay. 62 63 00:05:42,250 --> 00:05:49,970 So let's go ahead and add a new category, and let's say, let's call our category Home as we did before. 63 64 00:05:50,250 --> 00:05:50,590 Okay. 64 65 00:05:50,600 --> 00:05:56,830 So home is green. And let's add another one called Work. work is brown. 65 66 00:05:56,890 --> 00:06:01,510 Now, you'll see that the contrast between the text and the background is not great at the moment, 66 67 00:06:01,510 --> 00:06:05,940 but we're going to fix that a little bit later on using another of Chameleon's methods. 67 68 00:06:05,980 --> 00:06:09,180 But for now, this is pretty good. 68 69 00:06:09,220 --> 00:06:10,720 We're getting random colors. 69 70 00:06:10,870 --> 00:06:12,640 And here's the litmus test. 70 71 00:06:12,640 --> 00:06:20,990 If we terminate our app and go back into Todoey, look, Home is still green, Work is still brown. 71 72 00:06:21,220 --> 00:06:26,580 I completely agree with the color choices that have been randomly generated by my app. 72 73 00:06:26,590 --> 00:06:30,100 So did you manage to get the challenge? If you didn't, 73 74 00:06:30,190 --> 00:06:35,540 then this is a good time to go back and fix any bugs you might have and try to get it working. 74 75 00:06:35,560 --> 00:06:41,500 So in the next lesson-- Now, in the next lesson, we're going to be-- In the next lesson, 75 76 00:06:41,560 --> 00:06:46,490 we're going to be adding some gradient colors to our TodoListViewController. 76 77 00:06:46,510 --> 00:06:49,410 So if you want to see that, then I'll see you on the next lesson.