Structure, Protocol, and Class in Swift Language

Pasan Devin Jayawardene
3 min readMay 31, 2022

--

In this short article, I’ll discuss the differences between struct, proto, and class in Swift.

What is Swift?

Swift is a compiled programming language developed by Apple Inc. and the open-source community for general-purpose, multi-paradigm programming. Swift was created to replace Apple’s previous programming language, Objective-C, which had mostly remained unmodified since the early 1980s and lacked modern language capabilities.

Classes, Structs, and Proto are some of the most important terms in Swift.

what’s the difference between a Struct, a Prototype, and a Class?

Generally speaking,

  1. Protocols function similarly to interfaces.
  2. Classes are classes, like in Java/Android and pretty much any other language.
  3. Structs are similar to classes, but when sent from one variable/function to another, they are passed by value (copied). If you’re acquainted with C#, you’ll notice that it uses structs in a similar way.

Now Let’s take a look at them one by one.

1. Class

Classes are the building blocks of Swift 4’s versatile constructs. The user can define class attributes and methods in the same way that they can define constants, variables, and functions. Swift 4 makes it such that users don’t have to generate interfaces or implementation files when declaring classes. Classes can now be constructed as a single file in Swift 4, and external interfaces are automatically created once the classes are initialized.

The following are some of the advantages of using classes:

  1. The properties of one class are passed down to another class through inheritance.
  2. Typecasting allows the user to check the type of a class at runtime.
  3. Deinitializers are responsible for freeing up memory resources.
  4. The class instance might have several references thanks to reference counting.

2. Structure

A struct is used to store variables of various data kinds in Swift. It can also be viewed as an object’s template definition. It is capable of containing,

  • properties
  • methods
  • subscripts
  • initializers
  • protocol conformances
  • extensions

2. Protocol

A protocol is a set of methods, attributes, and other specifications that are tailored to a certain activity or piece of functionality. A class, structure, or enumeration can then adopt the protocol to offer an actual implementation of those criteria. Any type that meets the protocol’s requirements is said to conform to that protocol.

You can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can use, in addition to setting constraints that conforming types must meet.

Class vs Structure

Structures and classes are general-purpose, versatile constructs that serve as the code’s building blocks. The same syntax you use to describe constants, variables, and functions is used to define properties and methods to add functionality to your structures and classes.

Comparing and Contrasting Structures and Classes

In Swift, structures and classes have a lot in common. Both are capable of:

  1. Create properties to store data.
  2. Define the methods that will be used to offer functionality.
  3. Subscript syntax is used to define subscripts and provide access to their values.
  4. To set up their starting state, define initializers.
  5. Be extended in order to expand their capabilities beyond what is provided by default.
  6. Conform to protocols in order to deliver a given level of standard functionality.

Class vs Protocol

A protocol, in its most basic form, explains what an unknown sort of object can accomplish. It has two or three different sorts of properties, as well as procedures. But that protocol never includes anything inside the methods, or provides actual storage for the properties.

You can build extensions to your protocols that give default implementations of the methods in a more advanced form. However, you are still unable to provide storage for properties.

Classes, on the other hand, are tangible objects. They aren’t needed to embrace protocols, which means they don’t have to implement the required attributes and methods.

That is all the knowledge that I’m going to share with you today. Hope to see you in another article. Until then, Happy Reading!

-Pasan Devin Jayawardene-

--

--