Struct and Class in Swift

Updated 25 March 2021

Save

Struct and class are building blocks of our program’s code. Through it, we have defined the properties and methods to add functionality in it by using the constants, variables, closures, protocols, and functions.

Unlike other programming languages, Swift doesn’t require us to create separate interface and implementation files for custom structures and classes. In Swift, we define in a single file and the external interface that it is automatically made available for other code to use.

According to the very popular WWDC 2015 talk Protocol Oriented, Swift provides a number of features that make structs better than classes in many circumstances.

Classes

In OOP, a class is a blueprint from which individual instances are created. It is the reference types and every change in a reference type will modify the value allocated in that place of memory or reference.

It is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).

We define a class using the class keyword.

Structures

A struct is similar to a class in terms of definition and instance creation. It is the value types and that means that every change on them will just modify that value.

There is a very powerful feature of Swift, and they can help to make your code more reusable, more flexible, and less tightly coupled. And last but not least, they sharpen your skills as an app developer!

We define a struct using the struct keyword.

Comparing Struct and Class

Common factors between struct and class:

Classes  have benefits over structures :

Structures have benefits over classes :

Value Types and Reference Types

Value Type:

When we copy a value type (i.e., when it’s assigned, initialized or passed into a function), each instance keeps a unique copy of the data. If we change one instance, the other doesn’t change too.

Let’s take a look at an example:

Ranjit has some notes, and he gives it to Vicky. Vicky writes it down and now his has own copy. When he accidentally changes it, only his copy changes and not the original notes Ranjit has. Both Ranjit and Vicky have their unique copy of the notes.

Swift’s struct can change their mutability status:

let + struct = immutable = constant of value
It can not be reassigned or changed

var + struct = mutable
It can be reassigned or changed

Reference Type:

When we copy a reference type, each instance shares the data. The reference itself is copied, but not the data it references. When we change one, the other changes too.

Let’s take a look at an example:

Ranjit has some notes. He gives it to Vicky. Vicky doesn’t write down the notes for himself but instead remembers that Ranjit has it. When he needs those notes, he asks Ranjit. When Vicky accidentally changes in their notes, Ranjit’s notes change too.

Swift’s classes are mutable a-priory:

let + class = constant of address
It can not reassign and can change

var + class
It can  reassign or change

 

Why Choose Struct Over Class?

Consider the following example, which demonstrates 2 strategies of wrapping Int datatype using struct and class.

First, we can use one repeated value to better reflect the real world, where we get.

So Performance measured by using :

We have to initialize it.

As of Swift 5.0, Xcode 11.3, running Release build on iPhone 11 pro-Max, iOS 13.2.0, Swift Compiler setting is -O -whole-module-optimization:

Now we reflect the example in 10 fields for struct and class.

Now Performance measured  for 10-10 field  in class and struct: 

We have to initialize it for the performance.

As of Swift 5.0, Xcode 11.3, running Release build on iPhone 11 pro-Max, iOS 13.2.0, Swift Compiler setting is -O -whole-module-optimization:

Note: The difference is a lot less dramatic without whole module optimization. I’d be glad if someone can point out what the flag does.

We should default to using classes and use structures only in specific circumstances. Ultimately, we need to understand the real-world implication of value types vs. reference types and then we can make an informed decision about when to use structs or classes.

 

Conclusion

So, if you have any comments, questions, or recommendations, feel free to post them in the comment section below!

 

 

 

 

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