What to choose Struct or Class? in Swift

Updated 29 December 2020

Save

What to choose Struct or Class?

A question that arises in every developer’s mind “What to choose struct or class”

We would like to start with the very basic question “What are structures?” and “What are the classes” in Swift. Almost every OOP based programming languages include classes and structures. Here we will discuss the similarity and dissimilarity between classes and structures in the Swift language also we will discuss which one to choose among them.

Coming to our very first question what are structures and classes in Swift.

Structure

When we have to define the properties of common kinds or common types, it is advisable to declare a structure. Not only when we have common kind properties but also when we have multiple values to hold. Structures play an important role in holding data values.

For instance, we have multiple dishes in a restaurant, declaring variables for each dishes is a cumbersome task. Instead, we can declare a structure with a variable and hold the names of dishes in that variable.

Class

A class is a blueprint or template for an instance of that class. The term “object” is often used to refer to an instance of a class. When we create a project in the Xcode we land up with a default class named ViewController which is a subclass of the UIViewController.

 

While classes and structures are very similar, there are also several very important differences. The following is a list of some of the differences between classes and structures in Swift:
Type: A structure is a value type, while a class is a reference type

Deinitializers: Structures cannot have custom deinitialisers, while a class can.

Inheritance: A structure cannot inherit from other types, while a class can.

Creating a new Xcode project gives ViewController class by default, which is a subclass of UIViewController. That means ViewController class inherits UIViewController.

In Swift, classes and structures are more similar than they are in other languages, such as Objective-C. The following is a list of some of the features that classes and structures share:

Properties: used to store information in our classes and structure.

Methods: provide functionality in our classes and structures.

Initialisers: used when initializing the instance of our class and structure.

Subscripts: provide access to values using the subscript syntax.

Value versus reference types

Structures are value types. When we pass instances of a structure within our application, we pass a copy of the structure and not the original structure.

Classes are reference types, when we pass an instance of the class, a reference to the original instance is passed.

To illustrate the difference between value and reference types, let’s look at a real-world object: a Book.

We have a friend who wants to read a book, we could either provide multiple copies or share ours. If we provide multiple copies of the book, then any notes they made within the book would remain in their copy of the book and would not be reflected in our copy.

If we share our copy of the book, then any notes they made within the book would stay in the book when they return it to us.

Conclusion

Keeping in mind the similarity and differences between class and structure choosing among them entirely depends upon the project requirement.

Apple describes the selection of the struct and class to hold our data when we want to control the Identity of the information.

You can read more on this from here :

https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home