Struct Vs Proto Vs Class in Swift

Sachin Tharaka
4 min readMay 23, 2022


Welcome back. Today we are going to talk about the difference between classes, Structures(struct), and Protos (Protocols) in Swift programming language.

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Classes, Structs, and Proto are some key terms in Swift

Proto, Classes, and Struct in general,

  1. Protocols are effectively like interfaces.
  2. Classes are classes, like in Java/Android, and pretty much any other language.
  3. Structs are like classes, but they are passed by value (copied) when passing them from one variable/function to another. If you’re familiar with C# at all, its implementation of structs is very similar.

Technical aspect to the difference between a Struct, a proto, and a class in swift?

Classes in Swift?

In swift, classes are the building blocks of our programming code in which we declare our methods, properties, variables and etc. to extend the functionality of classes.

In swift, we can define classes using class keyword and define the properties, and methods in classes same as constants, variables, and functions.

The benefits of utilizing classes in a fast programming language are as follows.

We can use inheritance to pass properties from one class to the next.

We can use type casting to check the type of a class at runtime.

Memory management will be handled via Deinitializers in classes, which will release unneeded resources.

For each class instance, we may establish several references.

What is a struct in Swift?

In Swift, Struct (structure) is a special type of value, it creates a variable, to store multiple values, but the values are related to each other.

A struct in Swift is a value type that, just like classes, can contain:

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

Swift uses struct keyword to declare a Struct.

For example, information about an employee includes:

  • Employee number
  • Employee name
  • Position

You can create 3 variables to store the information above of employees. However, you can create a Struct to store all three information in a single variable.

What are protocols and how do they work in Swift?

Generally, a protocol:

Working with protocols is one of Swift’s most fundamental features. With protocols, you define “rules” that an adopting class must conform to. This principle lets you write decoupled, modular, and extensible Swift code.

  • Is a blueprint that a class or struct follows
  • Is a communication contract for unrelated objects to rely on
  • Defines methods and values

To understand how protocols work in Swift, let’s suppose we are building application software and must model the requirements to satisfy the application. We can either begin with a superclass and mold the relationship through inheritance or start with a protocol and mold the relationship through the implementation.

Swift: Protocol vs. Struct vs. Class

Classes vs Structs

  • Structs and classes may both define attributes for storing data and functions.
  • They can create subscripts to provide them access to values that use subscript syntax.
  • With init, they may create initializers to set up their initial state () They can be expanded (this is crucial!) using an extension.
  • For example, follow protocols to facilitate Protocol Oriented Programming. They can collaborate with generics to create types that are adaptable and reusable.

Classes support a few more capabilities that structs don’t have:

  • Classes can inherit from another class as you inherit from UIViewController to create your own view controller subclass
  • Classes can be deinitialized, i.e. you can invoke a deinit() function before the class is destroyed
  • Classes are reference types and structs are value types

protocol vs class

Basically, a protocol describes what an unknown type of object can do. But they don’t provide actual storage for any kind of data inside the methods or in the objects themselves. Instead, you can write extensions that provide default implementations of the methods.

A protocol/proto explains what an unknown sort of item can perform in its most basic form. It has two or three different sorts of attributes, as well as procedures. However, that protocol does not include anything inside the methods, nor does it allow actual storage for properties such as class.

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

Classes may be used to generate objects, whereas protocols are just typed declarations.

Classes and structs are real entities you can construct, but protocols are abstract specifications.

Proto vs. Struct:

In Swift, protocols provide communication across unrelated objects by defining methods and variables similar to those found in classes, enums, and structs.

Protocols are similar to interfaces, and Structs are similar to classes, except they are transmitted by value from one variable/function to the next.

Thank you for reading and we will meet again with another interesting article. #StaySafe #StayConnected



Sachin Tharaka

Software Engineering, University of Kelaniya, Sri Lanka