Classes vs Structs in Swift
Swift brings us classes and structs which both can look quite similar to one another. When should you use a struct and when should you prefer a class? Let’s dive into this topic.
What is a class?
A class in Swift is a reference type that can contain: properties, methods, subscripts, initializers, protocol conformances, extensions. See the below code for a class definition.
What is a struct?
A struct in Swift is a value type that, just like classes, can contain: properties, methods, subscripts, initializers, protocol conformances, extensions. See the below code for a struct definition.
Key Points
Structs are value types.
Classes are reference types.
Value Type: Value type is that type which created directly on memory. Every instance keeps a unique copy of data. When to assign or copy it created complete new data.
Reference Type: A type that shares a single data. When initialized once and assign to a variable or contact or a function its returns a reference.
Differences
Classes are reference types. When you assign a class instance to another variable and some of its fields are changed in that variable, you entirely change its memory address of that value. secondItem
is pointing to the same address as the firstItem
. So, the memory address of firstItem
is also changed.
Structs are value types. When you assign the value of a struct variable to another one, you are just passing a copy to the second. fourthItem
is pointing to the different memory addresses with the thirdItem
. When you change fourthItem
content, you are not dealing with thirdItem
’s memory space which is in a different location.
Classes can be inherited. We can use class inheritance to make the polymorphic structures.
Structs cannot be inherited. When AnotherItem struct tries to inherit from Item struct, Xcode gives an error saying that “Inheritance from non-protocol type Item”.
Classes can be a visual component. We can simply define a view controller class representing a screen, or it can be any type of view like UIView, UITableView, UIImage and etc.
Structs can represent data models. We simply make a http request and get the data to bind it to the User model. See the below example. The User struct conforms to Codable protocol which helps to decode and encode User data.
Use Classes;
- When you want to create subtypes from it.
- When it extends from another class.
- When you need Polymorphic architecture.
- When you draw a visual component.
- When you need the layer of some software architecture.
- When you need to control the identity of the data you’re modelling.
- When it will be deinitialized.
Use Structs;
- When you need to write data models of business logic.
- When it comes to fetching data from an API.
- When you only use its value, you don’t rely on its memory address.
- When you will not extend it.
- When you want protocols to adopt behaviour by sharing implementations.
Conclusion
To sum up, we understood that classes rely on their memory address, and structs rely on just their content inside. With this article, I hope you are now more familiar with these concepts.