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
We're done! Let's recap the entire course and call it a day.
-
0:00
Before we wrap up and talk about everything we've covered so
-
0:03
far it's important to address one point.
-
0:06
We learned of two ways we can encapsulate data, structs and classes.
-
0:10
When should we use either one?
-
0:13
A simple rule for now is to use structs to represent simple values as mentioned.
-
0:18
A coordinate point is just a set of values.
-
0:21
It doesn't really have an identity.
-
0:23
Classes, on the other hand,
-
0:24
are really good at representing more than just values.
-
0:28
Right now, it's hard to make that distinction since we just learned about
-
0:31
structs and classes to begin with, but honestly, really, don't worry about it.
-
0:36
We're going to revisit this topic again several times
-
0:39
until we have a good understanding of the differences between the two.
-
0:43
Now that we've seen two of the main types of objects we'll be working with in Swift,
-
0:47
let's go over the syntax and
-
0:48
concepts one last time before we take a code challenge to wrap up the course.
-
0:53
We have two main objects, structs and classes.
-
0:57
The syntax for creating either one is pretty similar.
-
1:00
We first start with a keyword that denotes what kind of object we want to create.
-
1:05
If it's a struct, we use the struct keyword, class for a class.
-
1:10
Both objects can contain stored properties.
-
1:13
In its simplest form, a stored property is a constant or a variable
-
1:17
that is stored as part of an instance of a particular class or structure.
-
1:22
We can make our stored properties mutable using the keyword var or
-
1:26
immutable using let.
-
1:28
Stored properties can also take default values as part of the class or
-
1:32
struct definition.
-
1:34
Using this blueprint of a class or
-
1:36
a struct definition, we can create an instance to use.
-
1:40
Creating an instance involves giving our stored properties initial values, and
-
1:45
we do this through a process called initialization.
-
1:49
Structs automatically get member wise initializers for stored properties, but
-
1:54
with classes, we need to define an init method.
-
1:58
An init method is simply a special instance method
-
2:02
that can take parameters like any function.
-
2:05
Inside the init method, we can set or
-
2:07
modify the initial values for any stored properties.
-
2:11
Both structs and classes can contain methods that we call on an instance.
-
2:16
These are known as instance methods and are written just like we write a function
-
2:21
except they're contained within the object definition and scope to it.
-
2:25
We access both stored properties and
-
2:27
instance methods by using dot notation with an instance.
-
2:32
All this is common between the two types of objects.
-
2:35
Here's where things start to diverge.
-
2:38
Classes support a mechanism called inheritance
-
2:41
where one class can be based off another and use the same implementation.
-
2:45
This leads to great code reuse and
-
2:48
allows us to extend objects in ways that narrowly scope their function.
-
2:52
For example, we don't need to build this one giant automobile class that has
-
2:57
the properties and methods of trains, buses, trucks, cars, motorbikes and so on.
-
3:03
Instead we can create a base vehicle class and then subclass that to create
-
3:07
more specific types, like bus, by adding properties and overriding methods.
-
3:13
To subclass a base class and create a new type, we declare a class like normal and
-
3:19
then add a colon and the name of the class we're inheriting from.
-
3:23
When we inherit from a superclass, we need to ensure that all the stored properties,
-
3:28
that is both for the subclass and
-
3:30
the superclass, are initialized when creating an instance.
-
3:34
We can create a new init method to do this if we want, but if we want to use
-
3:39
an initializer from the superclass like we did when we wrote the code,
-
3:42
we can simply override the init method and modify and set our values in there.
-
3:48
For now, there's a simple rule we're going to follow.
-
3:51
First, we provide values for the stored properties of the subclass.
-
3:55
Once this is sorted we'll call super init and
-
3:58
initialize any properties in the superclass.
-
4:01
The second main difference is that a struct is a value type
-
4:05
while a class is a reference type.
-
4:07
When you assign an instance of a struct to another variable or
-
4:10
constant, Swift copies the underlying values, creates a new instance, and
-
4:15
assigns it to the new constant or variable.
-
4:18
In contrast, when you assign an instance of a class to another constant or
-
4:22
variable, you are pointing to that exact same instance.
-
4:26
Any changes you make through one variable changes the original instance.
-
4:31
And that was a very quick high level run through on everything we've learned.
-
4:35
Don't worry, though, you'll have plenty of opportunities to put
-
4:38
all this into good use in the upcoming courses.
-
4:41
Now let's wrap this all up with a quiz and
-
4:43
a code challenge to see how well you know the material.
-
4:47
Remember, there are plenty of links included with the videos.
-
4:50
Make sure you go through this stuff,
-
4:52
because it will really help cement your knowledge.
-
4:54
Enjoy coding, and I will see you in the next course.
You need to sign up for Treehouse in order to download course files.
Sign up