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
Now that we are familiar with the syntax, let's create our first protocol.
-
0:00
For our next bit of code, let's define a struct which we'll
-
0:05
call User to represent a user in this hypothetical app of ours.
-
0:10
In our app, we're often going to refer to our user with their full name, so
-
0:14
we want to have a full name property.
-
0:17
We could simply add the full name, but that doesn't enforce it.
-
0:21
It's up to me to do that if I want.
-
0:24
So instead, let's make the user struct conform to the fully nameable protocol.
-
0:29
A protocol can be adopted by a class, struct or
-
0:34
enumeration and these objects can then define an implementation for
-
0:39
the requirements that are specified by the protocol.
-
0:42
So let's see what this looks like.
-
0:44
After the name of the struct User,
-
0:46
we're going to add a colon followed by the name of the protocol.
-
0:51
This looks a lot like how a class specifies a superclass or
-
0:55
base in its inheritance hierarchy, but that's not what this is.
-
0:59
You'll notice though in a moment that when you do this, you have an error.
-
1:03
When you specify that a type that is a class struct or
-
1:07
enumeration adopts a protocol, you have to provide an implementation
-
1:13
as described by the protocol to conform to it.
-
1:16
The FullNameable protocol states that you must have a stored property
-
1:21
called FullName, so let's add that.
-
1:24
We'll say, var fullName, of type String.
-
1:28
Notice the moment we add this, the error goes away.
-
1:32
Also take note of the fact that I specified the type here to be string.
-
1:37
Now, what if I change this to an integer?
-
1:39
Again, we get an error and if we click it,
-
1:42
it says that type user does not conform to the protocol.
-
1:46
And it says here,
-
1:47
the fix says protocol requires a property full name with a type String.
-
1:51
All right, so let's change that again.
-
1:55
Our protocol not only requires that you have a property called FullName but
-
1:59
also that it is of type string.
-
2:02
Any type that satisfies the requirements of a protocol
-
2:05
is said to conform to that protocol.
-
2:08
In this case,
-
2:09
we're saying that the User struct conforms to the FullNameable protocol.
-
2:14
Now, let's create an instance of user with the full name.
-
2:17
So, let’s say let user = User, and
-
2:22
we'll give it a name.
-
2:25
Like I said earlier, we could have simply added the stored property without having
-
2:29
to conform to a protocol.
-
2:31
The advantage of specifying the protocol is that it provides an expectation
-
2:36
of certain attributes or behavior.
-
2:38
By identifying that User conforms to full nameable,
-
2:42
we know that it must have a property called FullName.
-
2:46
Protocols can require that conforming types have specific instance properties,
-
2:51
instance methods, type methods, which we don't know about yet, and so on.
-
2:56
Remember, I said that a protocol only describes an implementation but
-
3:00
doesn't actually provide one.
-
3:02
So let's define another struct.
-
3:06
We'll call this one Friend.
-
3:09
Now, Friend is going to have, oops, let's go back up.
-
3:14
Friend is going to have a firstName stored property of type String.
-
3:21
A middle name, we'll make this non-optional
-
3:26
just to be easy and finally a last name stored property.
-
3:31
Okay, now let's adopt FullNameable again.
-
3:34
So struct Friend conforms, or adopts, FullNameable.
-
3:38
Again, we get an error because Friend now does not have a full name property.
-
3:43
Now, this time we're going to add a computed property for full name that
-
3:48
creates a full name automatically using values from the other stored properties.
-
3:52
We haven't learned about computed properties just yet.
-
3:56
Don't worry, we'll spend more time on them in the future.
-
3:59
A computed property, all you should know for now,
-
4:01
lets you create a property that determines its value through some computation,
-
4:06
hence the name computed property.
-
4:08
You cannot assign it a value directly and it has no storage.
-
4:12
So to do that, we'll say var fullName and then like a function,
-
4:17
we'll open a set of braces here and inside we'll say return and
-
4:21
we'll create an interpolated string here
-
4:27
to return the first name, middle name and last name.
-
4:33
Now we can create an instance of friend, so I'll say let friend equal.
-
4:41
And give it some values.
-
4:45
And then we can get a value for full name.
-
4:50
We can get this friend's full name by using the full name property.
-
4:56
Notice a very important thing here.
-
4:58
Both user and friend conform to the fullNameable protocol but
-
5:02
they differ in their implementation.
-
5:05
In the case of User,
-
5:06
we assigned a value to fullName when we initialized an instance.
-
5:11
Whereas with the instance of Friend, we assign a value to firstName,
-
5:15
middleName and lastName and then use those three properties to compute a full name.
-
5:21
This is an important characteristic of protocols.
-
5:25
All our FullNameable protocol says is that you need to have a full name property.
-
5:31
It doesn't say how you should get this property.
-
5:33
Can you assign a value to it or do you compute it from other properties?
-
5:37
None of that matters.
-
5:38
It specifies what should be implemented, but
-
5:41
does not require a specific implementation.
-
5:44
That is left up to the class structure or enumeration that adopts it.
-
5:49
We'll see how this is useful as we go through the course.
You need to sign up for Treehouse in order to download course files.
Sign up