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
Earlier we learned about type properties, but let's switch gears here and learn about type methods.

0:00
Value in reference semantics can be a bit mind boggling.

0:04
So let's switch to something more straightforward for now.

0:07
We started this course by looking at a simple concept, type properties.

0:11
Just like we can add properties to the type itself,

0:14
we can actually also add type methods.

0:17
But you knew this because we already wrote a type method before.

0:21
When we did that we used the keyword class.

0:23
But we just jumped ahead a bit, so let's dial it back and

0:27
we'll work our way through the different options.

0:29
So here we see some familiar code.

0:32
We've got a struct called Map, and

0:34
it relies on another struct here called Point.

0:38
Map also included a type or static property, origin.

0:43
That contained a point 0, 0.

0:46
Now let's say, given an arbitrary point,

0:50
we want to calculate the distance of that point from the origin.

0:54
We could make this an instance method.

0:56
But the computation does not depend on any values being assigned to stored

1:00
properties.

1:01
The only information we need given a point is the location of the origin,

1:05
which we see here, is a static property.

1:09
Now this could be a free function but

1:11
since it deals with an aspect of the map why not just add it to our type.

1:17
So to create a type method.

1:18
We start with the keyword static.

1:21
After that,

1:22
we're gonna write the method out just like we would with any instance method.

1:34
So here we see that this method takes a point.

1:37
And it's going to compute the distance from the origin and return us a double.

1:42
It's not all that complicated but there is a little math.

1:45
Let me just do that for you here.

1:59
And we can copy this exact same thing.

2:06
And we can just change it to vertical distance and our values to y values.

2:16
Okay, but we're not done yet.

2:17
We just have our horizontal distance and our vertical distance.

2:20
What we want really is a diagonal distance from our point to the origin.

2:25
Now to get the distance using these two values,

2:28
we're going to use the Pythagorean Theorem.

2:30
If you're unfamiliar with that,

2:31
we're first going to square both of these values.

2:34
Then we get the sum of those squares.

2:36
And then we return the square root of that sum.

2:40
To square the values, we could simply multiply a horizontal distance times

2:44
itself, but that would make our code look really ugly.

2:48
We could write a helper method square that takes a value and

2:52
returns a value multiplied by itself.

2:54
But if we try that and add it as an instance method,

2:57
you'll see that we won't be able to use it inside our static method.

3:01
The reason may not be obvious at first, but it actually makes a lot of sense.

3:06
You see, instance methods need an instance to be created before they can be used.

3:10
And inside of a type method, we don't have an instance.

3:14
Now we could add it as a second type method.

3:17
But unlike instance methods helper type methods, well they're not really a thing.

3:21
You only want methods strictly associated with the type's functionality.

3:26
Swift does let you do a cool thing however and

3:28
that is nesting functions inside of other functions.

3:32
So inside of our static method we can write a small helper function to use.

3:38
We can simply say func square.

3:43
And we'll take a value, which we're going to make a double.

3:49
And we want that to return to double, which of

3:54
course is just return value times value.

4:00
Which is squaring a number.

4:03
And now that we have this square method here, we can use it.

4:07
So we could simply return the square root.

4:12
See that function there that we're gonna use,

4:17
of square the horizontal distance

4:25
Plus square of the vertical distance.

4:41
Now the square root function we're using here is actually part of foundation.

4:45
So you may need to import it up here.

4:46
We don't in our case cuz we have UIKit already.

4:50
So notice that this example we're using a value type.

4:54
Now with value types we always declare both type methods and

4:58
properties with the keyword, static, like we do right here.

5:03
Static here refers to two things.

5:06
One, that this method is associated at the type level,

5:09
rather than the instance level.

5:11
And secondly, that the method is statically dispatched.

5:15
A simple way to think about static dispatch is that the compiler knows which

5:19
method you intend to call at compile time, rather than figuring it out at run time.

5:25
Hold onto that definition for a second.

5:27
By comparing it to dynamic dispatch in the next video,

5:30
we should be able to get a better understanding of both.
You need to sign up for Treehouse in order to download course files.
Sign up