Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Creating a Random Facts Struct
8:45 with Nicole HinckleyNow that we have our new swift file ready to go, it's time to create a data model. We will refresh our knowledge on how to create structs, and move our facts array into our model.
-
0:00
Now that we've got our new Swift file ready to go,
-
0:03
it's time to create our data model.
-
0:05
If you remember back from object-oriented programming,
-
0:08
two objects that we've already learned about are structs and classes.
-
0:12
I said in the past that we'd be creating structs for our data models.
-
0:16
Structs are great when the data model is simple and
-
0:19
when we don't care about which particular instance that we're working with.
-
0:23
Right now, the primary use for a data model is to hold an array of facts.
-
0:27
So a struct is perfect is perfect for that.
-
0:30
You could use a class here and have no problems.
-
0:33
But in any case where a struct will suffice, it's nice to just go ahead and
-
0:37
use them because of how simple they are.
-
0:40
The really great thing about structs is that we don't have to worry about creating
-
0:43
initializers.
-
0:44
Structs will create their own initializers automatically for
-
0:47
us, right out of the box.
-
0:49
Which saves us a little bit of coding.
-
0:51
To create a struct, we need to start with the struct keyword.
-
0:55
So struct, followed by the name that we want in upper CamelCase.
-
1:01
So let's go ahead and call our model RandomFactProvider.
-
1:05
Just as a side note, it's always good practice to keep your file name
-
1:10
as the same name as the model inside.
-
1:12
So here we have RandomFactProvider.swift, and our model name is RandomFactProvider.
-
1:18
Nothing's gonna break if you don't do this, but it's just good practice.
-
1:21
All right, and let's finish this off by adding some curly braces.
-
1:25
Remember that Swift objects like classes and
-
1:27
structs can contain stored properties to hold data.
-
1:30
Currently in our ViewController.swift file, our random color and
-
1:34
random facts arrays are stored property of our ViewController class.
-
1:39
I wanna grab our random facts array out here.
-
1:41
So let's highlight our facts, and make sure you get this little bracket here.
-
1:47
And cut it with Cmd+X.
-
1:48
And back in our model, let's just paste it right in using Cmd+V.
-
1:55
Nice, but now we've got some errors back in our ViewController file.
-
1:58
You can even see that right up here, that we've got three new errors.
-
2:02
So let's go back to our ViewController file.
-
2:04
And it looks like we're trying to subscript an array that
-
2:06
doesn't exist anymore.
-
2:08
We just moved our facts array out of our ViewController class and into a model.
-
2:13
So now, we'll just need to use our model instead.
-
2:16
To access the array in our data model,
-
2:18
we'll need to create an instance of our RandomFactProvider.
-
2:22
So let's go up top and create a new stored property by typing,
-
2:27
let randomfactProvider.
-
2:30
And then set that equal to an instance of our RandomFactProvider.
-
2:35
And you can see it pop up right here.
-
2:37
When we create an instance of an object,
-
2:40
we use these parentheses here to initialize
-
2:43
any value in our object that need to have their values set upon initialization.
-
2:47
Because our RandomFactProvider struct only has one stored property,
-
2:52
which is an array that already has values in it,
-
2:54
we can just use an empty initializer, just like we did here.
-
2:58
If any of that sounded really foreign to you,
-
3:00
we cover it all in the object oriented Swift course.
-
3:03
Now we can go ahead and replace the code we had earlier, to use our new data model.
-
3:08
So first up, in viewDidLoad, we'll say randomFactProvider.
-
3:14
And it's really important here that we use our randomFactProvider variable that we
-
3:19
created up top.
-
3:21
The capitalized one is actually just our struct.
-
3:24
And we're not trying to use that,
-
3:26
we're trying to use our stored property that we created up top.
-
3:29
All right, nice.
-
3:30
Now our random fact generator instance has a property that's called facts,
-
3:35
which is what we created in our model.
-
3:37
So using dot notation, we can access our facts array.
-
3:41
Nice, so let's hit Tab.
-
3:43
Right now this is getting us back our entire array.
-
3:46
So just like before,
-
3:47
we wanna use subscript notation to get the first item out.
-
3:50
So use brackets for subscript notation.
-
3:53
And we want the first value out, so we'll use a 0.
-
3:56
Okay, nice, so we don't have an error here anymore.
-
3:59
Let's check out what's wrong down in our showAnotherFunFact method now.
-
4:03
It looks like we're running into the same problem.
-
4:05
We're trying to access a facts array that doesn't exist anymore.
-
4:09
So let's get rid of all this code for our upper bound.
-
4:12
And for our upper bound now,
-
4:14
we need the count of our facts array from our model minus one.
-
4:18
So randomFactProvider.facts, to get our array out.
-
4:24
And then just like before, we need the count property.
-
4:28
And remember we need to minus one.
-
4:30
So this is exactly the same logic that we had before, but
-
4:34
now we're just using our model's array.
-
4:37
And finally, right here, we need to solve the same problem again.
-
4:40
We're trying to use our randomNumber to subscript an array
-
4:43
that doesn't exist here anymore.
-
4:45
So let's get rid of all this.
-
4:48
And do randomFactProvider, .facts, to get our array out.
-
4:55
And then to get our random value out of our array,
-
4:58
we're just gonna use this randomNumber again.
-
5:01
So all we've done here is make sure that anywhere we were accessing
-
5:04
our array before, we just replaced it to access our model's factor right now.
-
5:09
Our logic is exactly the same.
-
5:12
Let's go ahead and run our app, and make sure everything's still working.
-
5:17
Okay, nice.
-
5:18
So our first fact is set, right off the bat.
-
5:21
Which we told it to do in viewDidLoad.
-
5:23
And when we tap on our button, nice.
-
5:26
We're getting random facts every single time.
-
5:29
We can abstract our code away even more though.
-
5:31
Our RandomFactProvider isn't really providing a random fact for us right now.
-
5:37
It's just holding our array.
-
5:38
We can move the functionality of picking a random fact out of our ViewController and
-
5:44
into our fact provider.
-
5:45
So back in our model, let's go ahead and create a new function.
-
5:49
Let's name it func randomFact, and we don't want any parameters.
-
5:56
And we want it to return a randomFact out of our array.
-
6:00
So we'll return a String, and we'll close it off.
-
6:04
Let's go ahead and grab the randomNumber logic from our ViewController
-
6:07
by cutting and pasting again.
-
6:10
Back in our ViewController, here's our logic for
-
6:12
getting a random number out of our facts array.
-
6:15
So let’s cut it by using Cmd+X, and we'll paste it into our new function.
-
6:23
Now that we’re inside of our model and
-
6:25
not using an instance property to access our facts array,
-
6:29
we can just get rid of this first part here, where we're accessing our instance.
-
6:33
Delete that.
-
6:35
So now, right from within our model, we'll be able to create a randomNumber
-
6:39
between 0 and our local facts.count- 1.
-
6:44
Now all we need to do is use this randomNumber to return a randomFact
-
6:49
from our facts array.
-
6:50
So we can do return.
-
6:53
And then just like before, subscript our facts array with our randomNumber.
-
6:59
So now this randomFact method will return a random fact out of our facts array.
-
7:05
Finally, back in our ViewController,
-
7:07
instead of doing this little bit of logic in our ViewController, we can just set our
-
7:12
randomFactLabel.text equal to our randomFactProvider.
-
7:17
Which again, is our local instance of randomFactProvider.
-
7:20
And access our new property called randomFact.
-
7:23
All right, so let's run our app one more time and make sure that all still works.
-
7:29
Okay, everything looks good so far.
-
7:31
And if we tap on our button, awesome, it's still working.
-
7:39
Perfect.
-
7:40
This might have seemed like a lot of work for the same results as we had before.
-
7:44
But it's great to get into the habit of refactoring as early as you can.
-
7:48
Keeping your code readable and reusable should always be a high priority, and
-
7:53
something you're thinking about as you write your code.
-
7:56
If your ViewController file has a bunch of logic or collections in it,
-
8:00
chances are a lot of that code could be abstracted away by refactoring.
-
8:05
Employers and fellow coders are gonna want to see that you care about how clean and
-
8:09
organized your code is.
-
8:11
I can also speak from experience.
-
8:13
It's a lot easier going back to work on an old project that you haven't worked on in
-
8:17
a while, if you've taken the time to make it readable and organized.
-
8:22
Next, let's create our random background color provider.
-
8:26
If you're feeling comfortable, I'd encourage you to try to create this second
-
8:29
model called random background color provider by yourself, before moving on.
-
8:34
I've left an array of colors down in the teacher's notes if you wanna
-
8:37
give it a try.
-
8:38
If you're not ready for all that, that's totally okay.
-
8:41
Maybe just try to think about the steps we might take to make our model.
You need to sign up for Treehouse in order to download course files.
Sign up