Classes, Structures and Protocols in Swift

Nimantha Gayan
6 min readAug 31, 2022

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns. The goal of the Swift project is to create the best available language for uses ranging from systems programming, to mobile and desktop apps, scaling up to cloud services. Most importantly, Swift is designed to make writing and maintaining correct programs easier for the develope

Swift Structures

Swift structures are the flexible basic building blocks of the programs. The “struct” keyword is used to define structures. By using structures, you can define constructs methods and properties.

  • Structure need not require implementation files and interface.
  • Structure allows us to create a single file and to extend its interface automatically to other blocks.

In Structure the variable values are copied and passed in subsequent codes by returning a copy of the old values so that the values cannot be altered

Swift Classes

Classes in Swift are building blocks of flexible constructs. Similar to constants, variables and functions the user can define class properties and methods. Swift provides us the functionality that while declaring classes the users need not create interfaces or implementation files. Swift allows us to create classes as a single file and the external interfaces will be created by default once the classes are initialized

Classes refers multiple constants and variables pointing to a single instance. To know about the constants and variables pointing to a particular class instance identity operators are used. Class instances are always passed by reference. In Classes NSString, NSArray, and NSDictionary instances are always assigned and passed around as a reference to an existing instance, rather than as a copy.

Swift Protocols

Protocols provide a blueprint for Methods, properties and other requirements functionality. It is just described as a methods or properties skeleton instead of implementation. Methods and properties implementation can further be done by defining classes, functions and enumerations. Conformance of a protocol is defined as the methods or properties satisfying the requirements of the protocol.

Protocol is used to specify particular class type property or instance property. It just specifies the type or instance property alone rather than specifying whether it is a stored or computed property. Also, it is used to specify whether the property is ‘gettable’ or ‘settable’.

Property requirements are declared by ‘var’ keyword as property variables. {get set} is used to declare gettable and settable properties after their type declaration. Gettable is mentioned by {get} property after their type declaration.

Class vs Struct in Swift

When we need to decide how to store data and model behavior, there are two choices: Classes and Structs. In this article, I will explain which type should be preferred in different situations.

What they can offer in common.

  • They both define properties to store values. If we want to create an object with some properties and behaviors, we can use both of them.
  • Both of them define subscripts so we can reach their values via subscript syntax.
  • Classes and structs have initializers to create instances with initial values.
  • Conforming to protocols are also in common. Some protocols may be class only so only classes can conform to that protocol, otherwise both can conform it.
  • Finally, they can be extended.

Classes yet have some extra capabilities that structs don’t have.

  • They can inherit another classes characteristics.
  • A class can free up assigned resources by deinitializers.

When to use classes?

We need to use classes when we need Objective-C interoperability. If we use an Objective-C API that receives data from our side, those data must be a class because Objective-C doesn’t have structs.

Another use case for classes is when we need to control identity. If we need an instance of an object through the app and we want to control its identity, classes are the solution.

When to use structs?

Almost anytime. We must use structs by default to represent common kinds of data. Structs in Swift are powerful and have many features. They have stored properties, computed properties and methods. Also, they can conform to protocols and gain their behaviors. Many of types from Foundation and the Swift standard library are structs; for example strings, arrays, numbers and dictionaries.

Structs help you track a portion of your code since they are value types. We only need to focus to the area where the struct is used, because there is not any object that points to struct’s address and manipulate it. Otherwise we need to look at possible places that may change our object’s value.

Apart from classes, we should use structs when we don’t control identity. Think about fetching data from server, there may be same objects but since we are not controlling identity, we better use structs.

difference between classes vs structs

Struct and Classes are pretty similar in Swift. Still, there are quite a lot of important differences to be aware of.

  • One of the most important differences is that a struct is a value type while a class is a reference type.

Struct is the value type object, it is the member-wise initializer. It is called a value type because it passes the value to the function. When you use the struct whole thing is duplicated. It points to the value of the object and then shows the value.

Class is the reference type of object. It passes the reference data to the function. When class is used each object points to the same original object if the developer change one they all will change. It shows the reference to the real object.

  • Structs are allocated in Stack memory.
  • Reference of the class object can be created on the Stack but the properties of the class object are stored in the heap.
  • A class can inherit the property or characteristics of another class. But in struct, it is not possible.
  • Deinitializers are only available on class types. We can de-initialize the class resource at any instant.

difference between protocol vs classes

When writing protocol-oriented Swift, protocols and classes become fairly similar, but they are never the same.

In their basic form, a protocol describes what an unknown type of object can do. You might say it has two or three properties of various types, plus methods. But that protocol never includes anything inside the methods, or provides actual storage for the properties.

In a more advanced form, you can write extensions on your protocols that provide default implementations of the methods. You still can’t provide storage for properties, however.

In comparison, classes are concrete things. While they might adopt protocol, say they implement the required properties and methods they aren’t required to do that. You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

Advantages of protocols in Swift

From the above examples, we can see why protocols are useful and why Swift uses the protocol-oriented paradigm. The advantages of using protocols manifest in the following ways:

  • Code clarity
  • Reusability
  • Separation of classes

protocols in Swift offer communication between unrelated objects where we define the methods and variables observed in classes, enums, and structs. Because Swift embraces the protocol-oriented paradigm, we can model our system before defining classes, structs, or enums, making the process more efficient.

I hope now you have clear understand about classes , struct and protocols in swift and when to use them and what are main differences of them.

Thank you for reading!!!

--

--

Nimantha Gayan

Software Engineering Undergraduate, University Of Kelaniya