Methods are simply functions that live in a type. Classes and structures can have instance methods; and in this module, you’ll learn about creating methods in your structure or class. For instance, a structure could have many properties but also a method with logic that returns a value from a calculation concerning those properties.
You’ll learn how to use instance methods—which is the functionality of a method within an instance—implement methods, and call them on instances of your type. You’ll also learn about the differences methods face within structs compared to classes. Structs are, by their nature, value types, which means you cannot change or modify their properties with a method. You can, however, use a mutating behavior for that method, which can change the property of the structure. Classes are reference types, so instead of mutating a method in a class, you’ll learn how to override methods from an inherited class.
Learning Objectives
- Recognize the different between methods and functions
- Explain instance methods
- Identify how to override a class’s method
Skills you’ll gain
Function ModuleInstantiationJava ModuleMethod CallMethod EngineeringSwift (Programming Language)What You'll Learn
- Recognize the difference between methods and functions
- Explain instance methods and how they provide functionality within an instance of a type
- Create and implement methods in your structures and classes, then call them on instances
- Use mutating behavior to change a property of a structure
- Override methods inherited from a class
Key Takeaways
- Methods are functions that live in a type, and both classes and structures can have instance methods.
- Instance methods provide functionality within an instance of your type, where a structure could combine properties with a method that returns a calculated value.
- Because structs are value types, you cannot modify their properties with an ordinary method, but a mutating method can change a property of the structure.
- Because classes are reference types, you override methods from an inherited class rather than using mutating methods.
Frequently Asked Questions
What does this Swift module cover?
It covers creating methods in your structures or classes, including instance methods, implementing and calling methods on instances, mutating methods for structs, and overriding methods from an inherited class.
What is the difference between methods and functions in this course?
The course explains that methods are simply functions that live in a type, and one of its learning objectives is to recognize the difference between methods and functions.
How do methods differ between structs and classes in Swift?
Structs are value types, so you cannot change their properties with a method unless you use a mutating behavior; classes are reference types, so instead of mutating you override methods from an inherited class.
What lessons are included in this module?
The module includes three lessons: Introducing Methods, Mutating Methods, and Overriding Methods.
Who is this course for?
It is a Swift beginner module for learners who want to learn how to create, implement, and call methods in structures and classes.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hello, my name is Mark Nair. And in these lessons you'll learn about methods, mutating methods and overriding methods. When we use the word method we're just talking about a function that we've given to a type. Functions live outside of a type, methods are just functions that live inside of the type. For a good example, let's just build one. So I'm gonna make a struct called city. And inside of the struct, I wanna calculate the distance between a place to another place in kilometers versus miles. So let's just try it. First, we'll make the struct, keyword struct. Then the name of the struct, I'll just call it city. Early braces. Now let's add some properties. I'll have two properties, a name of the city and the distance from Santa Fe in miles. So we'll just say that this is just about the city Santa Fe. So first property. So the name will be of type strength, second property and I'll be very explicit about this. I'll just call it distance from Santa Fe in miles. And I'll make this a double. So far, so good. The struct now is the same thing. A basic struct two properties name and distance from Santa Fe in miles. Now let's add a function inside of the struct to show you how methods really work. Same thing here, keyword func for function. I'm gonna name the function distance from Santa Fe in KM. The idea is I will take the miles that someone types in is the property of distance from Santa Fe in miles, convert that to kilometers. And then I wanna print out the calculation, the result. So let's make that function do the work. So first I'm gonna set up a new constant called distance in KM. I'm gonna make that equal to distance from Santa Fe in miles. Now to calculate miles into kilometers we'll just use a value. I'll multiply this by that value, which is all right. 1.609344, just a constant value. Then I will print. So this will be the name of the city. This is the distance in kilometers from the calculation from line six. This is a very Santa Fe centric. Little bit of code here. Looking good. Now, how do I activate, call, use the method? Well first of all, let's go ahead and say, I'll make a new constant called city one. So here are the properties that we have in the struct. I'll accept that. And I'll just say Albuquerque in the distance from Santa Fe in miles is about 64 miles. All right. So now I have the properties Albuquerque and it's about 64 miles from Santa Fe. But even still we don't have any values here. And if I look on this area over here, I don't see anything. I peek into it. I really just see. It's just these values. To really get to the method, I'll type in the name of that constant then dot. And then I have these pop up and you can see here distance from Santa Fe in miles. And there's a P, which means the property. So there it is. The property on line three. Name is a property. So there's a property in line two but here there's an M that stands for method. Distance from Santa Fe in kilometers. I'll select that. Now, when I run it, the debug council, Albuquerque is 1.2.998016 kilometers from Santa Fe. That's very specific. Let's round that instead. Now, rounding I'll place it right here on line six. I'm gonna round this calculation. So I'll just use the keyword round. I need to put the calculation in parenthesis to make it work. I'll see an error here. Cannot find round in scope. Round is an extension in swift. We need to bring in some help to make sure that our code understands it. And that help is a framework called foundation. Come up here to the top. I'm gonna import, which is another keyword. You can import frameworks and libraries and all sorts of things. But this case I want to import foundation. Now doing that tells our code, now I understand what round is. It's part of foundation. Round is going to round the calculation here and there it is. Albuquerque is 103 kilometers from Santa Fe. Option clicking round shows us this kind of weird thing but we know what this is. We are spitting out a double and in the parameters here we can see exactly what we're dealing with. Let's do something for another city. Now, what we could do is say city one dot. I'll just say name, is now equal to, let's make it. City one dot distance from Santa Fe in miles is equal to, it's above 420, 419 miles. The problem here is I have let city one. I'll change that to VAR. You'll run into that kind of business all the time. I'm trying to change values inside of this, but I said it's a let and I can't. It won't let me, but now I have it. It won't let me, I won't let me. I've changed the values here of the properties. Now let's take a look at our method. City one distance from Santa Fe in kilometers. Boulder is 674 kilometers from Santa Fe. And there we have it. It's a very simple idea. It's just that the function here now lives inside of a type. The type in this case is the struct that we've called city. It could very well be a class. So just think in terms that a method is just a function, just placed someplace else. Thanks for watching. In the next lesson you learn about mutating methods.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.