KnowledgeCity

Swift Beginner: Methods

Learn about Methods.
Preview the first lesson free — get full access to all 3 lessons.

To view this video please enable JavaScript.

Course: On-Demand
Beginner  Provider Mark Nair  3 Lessons ·  20m  in Arabic, German, English, Spanish, French, Portuguese, Chinese Simplified 

Course Description

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. 

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.