All right. 1
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
you have some bugs or you just want to see how I might choose to do it. 3
Now, in our challenges, I like to say that there's no correct way, 4
there's only different paths. 5
And as long as it leads you to the result that we wanted, then you have succeeded in the challenge. 6
So let's have a look at how we might do this. 7
Let's think about it logically. 8
What we need to happen is that when we create a new category, it needs to be associated with a new piece 9
of data, not just a name or a list of items, but it now needs a color. 10
So in our category class, we're going to have to need to add a new property that is called to say colour 11
and this is going to be set as a string data type because we're going to save a hex string into this 12
property. 13
Now, let's head back over to our CategoryViewController and think about where is the first time point 14
where we create our categories. 15
Well, that's, of course, inside our addButtonPressed method, and we need to be in here because when we 16
create a new category, we give it values for all of its properties including the category's name. 17
And now, we'll also need to give it a colour when we create it. 18
So we're going to set this to equal UIColor.randomFlat. hexValue. 19
And this is now a string, as you can see over here. 20
So that string gets saved into that property colour and gets persisted when we call save to our newCategory. 21
Now, once it's been saved, then we'll need to retrieve it, 22
right? 23
So when we retrieve--so we retrieve our data when we call loadCategories inside viewDidLoad. 24
And loadCategories pulls out all of the objects that are of the type Category, and it gets put into this array 25
or result's container called categories. 26
Now, when do we need to use that information? 27
Well, we use it here to change the textLabel of the cell and we're also going to use it here to change 28
the color of the cell. 29
So we're going to set it to equal a UIColor using a hexString. And this placeholder is telling 30
us that we need to put a String inside here. 31
So let's go ahead and do that. 32
So what is our string going to be? 33
Well, it's going to be the categories at indexPath.row.colour. Now, the first issue we get is that 34
categories is an optional, 35
so we would have to use optional chaining in order to make sure that we don't try to use a nil value. 36
So if categories is not nil, then we're going to grab the category at indexPath.row, and then we're 37
going to tap into the colour property. 38
Now, colour may or may not have a value. 39
So if it doesn't have a value, then we're going to do the same thing as we did up here. 40
We're going to say if there is no value, then here is a default value. 41
So the default value that we're going to give as the hex code is we're going to go into 42
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
at that colors hex color inside the RGB Sliders 44
under this particular tab. So we're going to copy that code over and we're going to paste it inside here, 45
so that we can use it as the default hex color. 46
So if your category doesn't have a color yet, then we're going to use a default value for the color. 47
So you can shorten these two lines of code if you wish by saying if let category = categories? 48
optional chain to indexPath.row. 49
And if that is not nil, then we're going to call these two lines of code. And instead of using categories 50
at indexPath.row, we're simply going to use our new constant called category, and we can replace 51
it in those two places. 52
All right, so let's give it a go and see if we manage to solve that challenge. 53
So the first thing that will happen when you run your app is your app is going to crash. 54
And that's because if we open up our Realm Browser, you'll remember that category already has three items. 55
Now, these three items don't have the third property color yet, but color is going to be used inside our 56
app. 57
So when this mismatch happens, you get this particular error: 58
"Error Domain=io.realm Code=10. Migration is required because Category.colour has been added." 59
So, what we have to do, if you remember, is you will have to hold your app, delete it, and rerun your app. 60
And that wipes all of your data, so that everything is consistent. 61
Okay. 62
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
Okay. 64
So home is green. And let's add another one called Work. work is brown. 65
Now, you'll see that the contrast between the text and the background is not great at the moment, 66
but we're going to fix that a little bit later on using another of Chameleon's methods. 67
But for now, this is pretty good. 68
We're getting random colors. 69
And here's the litmus test. 70
If we terminate our app and go back into Todoey, look, Home is still green, Work is still brown. 71
I completely agree with the color choices that have been randomly generated by my app. 72
So did you manage to get the challenge? If you didn't, 73
then this is a good time to go back and fix any bugs you might have and try to get it working. 74
So in the next lesson-- Now, in the next lesson, we're going to be-- In the next lesson, 75
we're going to be adding some gradient colors to our TodoListViewController. 76
So if you want to see that, then I'll see you on the next lesson.