Methods in Swift

Harshvardhan Arora
5 min readJun 12, 2022

--

Photo by Todd Quackenbush on Unsplash

You’ve learnt about functions, and you’ve learnt about data types like structs, classes and enums. Methods are simply functions that are associated with a type. Similar to properties, we can have instance methods and type methods. The entire playground for these tutorials can be found here!

Instance Methods

As the word suggests, these methods belong to instances of your class, struct or enum.

As you can see, the syntax for creating instance methods is exactly same as creating functions. The only difference being a method is always inside a type.

self

Every instance of a type has a property called self that refers to the instance itself. You can use that property inside instance methods of that type. You don’t need to use this normally, as you can see from the above example when we refer to numberOfEmployee inside out method. So when would you need it? Let’s take a look at an example —

Note: The above method was defined inside the Company class defined above

Inside the method, the parameter name takes precedence over properties defined in the class. Thus if you did not use self, Swift would assume you are trying to modify the parameter itself instead of the instance property. (which will throw an error)

Value type vs Reference type

You already understand the difference between value types (structs and enums) and reference types (classes) from the article on Structs and Classes. Now there is an important difference to understand here —

By default, instance properties of value types cannot be modified by instance methods. The same is not true for reference type, where instance methods can modify instance properties.

We need to add mutating keyword to that method to allow it to change instance properties of that type. Let’s look at an example —

Changing self

Mutating methods can assign a new instance to the self property. The same is not possible for classes which are reference types.

Here we define a structure Aquarium that has a property named numberOfFish. We have a mutating function that takes in fishes as its parameter and then goes on to set the self property to a new instance of Aquarium using the new number of fishes. Let’s try to verify if it actually creates a new instance or not —

If you run this, most of the times you will observe that the someRandomFish property has changed even though there is no method which tries to change it. This is because on creating a new instance of the Aquarium struct, we end up generating a random number for our someRandomFish property.

Type Methods

Just like properties can be instance and type properties, methods can be too. We have already looked at instance methods which are called from instances of our type. Now we will look at type methods which work quite similar to type properties.

Definition

Defining a type method inside a struct is exactly same as defining a type property. Let’s see an example of a type method inside a class

Now this is where we see a slight difference, we can use both static and class keywords to define type methods in a class. What is the difference? Class methods can be overridden by sub-classes.

Usage

Just like type properties, type methods can only be called upon from the type itself and not its instance. You can use them to modify type properties but not instance properties. You can also use them to generate an instance of your type. Let’s look at an examples —

We have a class named MoneyExchange which has a type property named rateMultiplier, a type method named changeMultiplier and an instance method named calculateMoney. Let’s see this in action —

Let’s go line by line to see what is happening here —
75 — We create an instance of our class MoneyExchange.
76 — We call the instance method calculateMoney and supply 10 as an argument. The method then multiplies it with the type property. rateMultiplier which is currently set to 50 and thus we get the output 500
79 — We call the type method changeMultiplier to set the type property rateMultiplier to 100.
80 — We again call the instance method calculateMoney and supply 10 as an argument. This time rateMultiplier is set to 100 and thus we get the output 1000.

This example alone should provide you a lot of insight as to how type properties and type methods work together.

That’s it! We covered a lot of ground here. Give yourself time to absorb all of it, try to write out the code yourself, look at the playground itself in case you get stuck. Feel free to reach out to me on LinkedIn in case you have any questions.

Up next we will be looking at Subscripts in Swift. Stay tuned!

It takes up a lot of my energy and resources to create these articles for you. You can support me in this journey by buying me a coffee. If it is not possible for you to contribute monetarily at the moment, it would be great if you could share this article with your network! I hope to keep bringing you such content to help you in your iOS development journey.

--

--

Harshvardhan Arora
Harshvardhan Arora

Written by Harshvardhan Arora

Simplifying Swift concepts for You

No responses yet