Struct, Class & Protocol in Swift
In this article, I going to talk about the differences between struct, protocol, and class in Swift Language. Most importantly, the appropriate usage, especially when it comes to a Struct versus a Class.
Before I dive into the differences, let’s look at them individually.
What is a Class?
Swift’s classes serve as the foundation for flexible constructs. The user can provide class attributes and methods, just like they can with constants, variables, and functions. Swift gives us the ability to not need users to generate interfaces or implementation files when declaring classes. Swift enables us to construct classes in a single file, and once the classes are initialized, the external interfaces will be established by default.
What is a Struct?
Structs are a fundamental aspect of Swift, and they can help increase the reusability, flexibility, and loose coupling of your code. They can also help you sharpen your skills as an IOS app developer!
What is a Protocol?
A protocol describes what an unknown type of object can do. It defines the blueprint of methods or properties that can then be adopted by classes or structs. However, a protocol never includes anything inside the methods or provides actual storage for the properties.
Now since we have a basic understanding of the three concepts, let’s try to differentiate them.
First, let’s examine the similarities between classes and structs:
Similarities between a Class and a Struct
- Structs and classes both can define attributes to hold values.
- Both can define methods to provide functionality.
- Both can define subscripts to provide access to values with subscript syntax.
- Both can define initializers to establish their starting state, using init().
- Both can be extended.
- Both can conform to protocols.
With the similarities in mind, let’s dive into the differences.
Differences between a Class and a Struct
Classes offer a few more features that structs do not, including:
- Classes can derive from other classes.
- Classes can be deinitialized, which means you can call the deinit() function before destroying the class.
- A Class must have an initializer but it’s not required for a Struct.
But most importantly they differ because of one crucial fact. This is that, in Swift, Classes are reference types and Structs are value types.
But exactly does this mean?
Pass by Reference VS Pass by Value
Pass-by-reference, each instance of a reference type that is copied shares the data. Although the reference is copied, the data it refers to is not. All instances changes when the other is changed. This is because the memory address is passed to that function.
Pass-by-value involves copying everything of an instance to a different memory address location. A different copy of the data is kept in each instance. It only has access to that one instance when updating or accessing it. As a result, the values of the other instances are unaffected. Only that one instance is affected by the changes.
So, when you duplicate a struct, you get two distinct copies of the data. When you duplicate a class, you get two references to the same piece of data.
When should you use a Class?
It’s best to use Classes in these scenarios:
- If you require Class specific features which are only unique to Classes.
- If you want to interop between Swift and Objective-C.
- When copying or comparing instances doesn’t make sense.
- When the lifetime of an instance is tied to external effects.
- When instances are merely conduits for external states.
When should you use a Struct?
On the other hand, it’s recommended to use Structs in these scenarios:
- Use structs for simple data types.
- In a multi-threaded environment, for instance, with a database connection that’s opened in a different thread, structs are safer.
- When the properties are mostly value types too.
- When you don’t need Inheritance.
Difference between a Protocol and a Class/Struct
Swift is protocol-oriented programming langue and even Apple encourages programmers to use protocol than class. Classes and structures are concrete objects in contrast to protocols, which are more of an abstract definition.
Perhaps put more simply:
- “class” defines what an object is.
- “protocol” defines a behavior the object has.
Both classes and structs can conform to protocols.
Thank you for reading!