The Class, the Struct, and the Protocol in Swift

Hasini Samarathunga
4 min readAug 10, 2022

A topic that’s lately quite common in interviews.

In this article, we’re going to take a look into structs vs classes vs protocol. When do you use a class, and when do you use a struct? What’s the difference between a struct, a class, and a protocol, and how does that affect practical iOS development?

Let’s start with the classic Struct and the Class.

Struct vs Class

Are they any different? I mean they’re so alike! We hear that it’s best to use structs by default, but why? And when should you use classes, then?

All these burning questions will be answered in this article. First, let’s take a look at what features classes and structs have in common:

Similarities between the two

  • Both can define properties to store values, and define functions.
  • With init(), both can define initializers.
  • They can be extended.
  • They can conform to protocols.
  • Finally, Both of them define subscripts so we can reach their values via subscript syntax.

Now that we know how similar they are, we can identify their differences, but,

Before we dive into the difference between a Class and a Struct we need to learn how to differentiate between passing by reference versus passing by value.

Pass by Reference — In pass-by-reference, the memory address is passed to that function. When you copy a reference type, each instance shares the data. When accessing or modifying one instance we can modify the shared value of all the copied instances by using its reference.

Pass by Value — In pass-by-value, all values of an instance are copied to another location of the memory. Each instance keeps a unique copy of the data. When accessing or modifying one instance, it accesses only that instance. Thus, there is no effect on the other instances’ values. The changes are made to only that instance.

With that in mind let’s move on to our main topic.

Difference between a Struct and a Class

In Swift, structs are value types whereas classes are reference types. When you copy a struct, you end up with two unique copies of the data. When you copy a class, you end up with two references to one instance of the data.

Let’s see this in a simple code example.

Here we have a simple Person Class with two attributes. We create a person object with this class named “Mary Jane”. Then copy this object into another person's object. When we change the name of that object, the original person's names change as well.

The above code prints “John Doe”, because Classes are reference types.

Next, let’s take look at a Struct.

The above code does the same as before but this time using a Struct instead. This time it prints “Mary Jane” because Structures are value types.

One other thing that you may have noticed is it’s a must for a Class to have an initializer but it’s not required for a Struct.

So when to use which?

Whether to use a Struct or a Class has always been a subjective argument. Generally, the rule of thumb is:

Use Structures— if you have something that depends on the value of data. For example, a Model/ViewModel. Or almost anytime really.

Use Classes — if you need Objective-C interoperability or if you need to control the identity of an object.

Personally, I prefer to use Struct whenever I can. In a lot of frameworks, Apple themselves pushes using Structs. Even Swift UI is heavenly built on Struct. But in the end, it’s mostly up to you.

What is a Protocol?

So where does the protocol comes into this mix? We have heard of writing protocol-oriented Swift, but what exactly is the relationship between protocol and a class?

In a nutshell, a protocol describes what an unknown type of object can do.

You might say it has two or three properties of various types, plus methods. But that protocol never includes anything inside the methods or provides actual storage for the properties.

Even if we can provide default implementations of the methods with some extensions on the protocols, it still can’t provide storage for properties.

Protocol vs Class and Structure

  • In comparison, classes and structures are concrete things, while Protocols are more of an abstract definition.
  • You can create objects from classes and structures, whereas protocols are just definitions.

Conclusion

In short, structs are value types whereas classes are reference types. And a protocol simply defines the blueprint of properties and methods. And use these protocols in classes or structs.

And as we discussed in this article, although there’re appropriate instances where you have to choose between them, it’s up to you to decide whether to use classes or structs.

Hope you found this article helpful! See you in another article!

Thank you!

--

--