Swift Class vs Struct – How to Choose?

0

Class and Struct in Swift are similar in many ways. Same functionality can often be achieved using either of them. Therefore, it might seem confusing to the developers new to Swift to decide why two similar kind of solutions exists in the first place and which one to use for a specific problem.

I had the same confusion when I started programming in Swift and I did some research and created comparison tables to clear my understanding. I am sharing that with you so that it saves some effort from your side.

General guideline by Apple is to try to use structures over classes as they are simpler and will serve the purpose for most of the scenarios. In practice, most of the custom data items (value objects) we define will be structures and enumerations while processing logic and algorithms can be implemented as classes where there is an hierarchical relationship between the objects.

Structures are passed by values. Passed by value means, a clone of the object is created when it’s assigned to a variable or constant, or when it’s passed to a function. We call this passing by value.

This means that any structure we create and any property they have which is a value type are always copied when they are passed in code. All basic data types in Swift are implemented as struct.

This is very convenient way of working with value objects. For the programming languages that only supports passing objects by reference (e.g. Java) we need to write custom methods or utilities to create a copy object with different reference which will contain same inner values.

Following table contains a comparison between the features supported by Struct and Class:

PropertyClassStructComments
Inheritance SupportedYesNoStructs can participate in protocol inheritance but not class inheritance.
Default InitializerNoYesAll structures have an automatically generated member wise initializer, which you can use to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the member wise initializer by name.
Pass by Reference/ValueReferenceValueWhen one class variable (object) is assigned to another variable or passed as parameter to a method, this is actually the reference of the class variable that is being passed, not a cloned copy of the object. However, when a struct variable is assigned or passed as input, a copy of the struct is created and passed. Changing any property in the new variable does not impact the original struct.
Explicit mutating keyword requiredNoYesProperties of the struct are immutable by default i.e. once created/assigned, the properties cannot be changed. If we want to change any of the properties after creation then ‘mutating’ keyword needs to be specifically mentioned before the property variable declaration.
Define properties and methodsYesYes
Conform to ProtocolsYesYes
ExtensionYesYesExtensions can add new functionality to a type, but they can’t override existing functionality.
Type castingYesNo
De-initializersYesNo
SubscriptsYesYes
Class vs Struct

As you can see, most of the features supported by a class are also supported by struct without the complexity of inheritance and benefit of being immutable which removed a lot of complexities from code.

So, you should use Structures whenever possible and only use classes where you need to define hierarchy of objects that primarily encapsulates processing logic or algorithms. I home this article has helped you in understanding the difference between Struct and Class in Swift and when to use which one. Please let me know in the comments section below.

For more information on this, please refer to official Apple developer guide at https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Positive SSL
Exit mobile version