Structure, Protocol, and Class in Swift: The differences explained

Rasalingam Ragul
3 min readMay 24, 2022

--

In this Article, I am going to explain the difference between struct, proto and class in swift.

What is Swift?

Swift is a multi-paradigm, general-purpose, open-source programming language for iPadOS, macOS, tvOS, watchOS, and iOS development. It was created by Apple in 2014 to give developers a powerful language to develop iOS apps.

What is a class in Swift?

A class in Swift is a reference type which can contain:

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

It’s often described as a template definition of an object, like in the following Article instance definition:

What is a struct in Swift?

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

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

It can also be seen as a template definition of an object, like in the following Article instance definition:

What is a Proto?

Generally, a protocol:

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

It’s often described as a template definition of an object, like in the following Article instance definition:

Class vs Structure

Structures and classes are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your structures and classes using the same syntax you use to define constants, variables, and functions.

  • Classes can inherit from another class, as you inherit from UIViewController to create your own view controller subclass, but struct can’t.
  • Classes are reference types and structs are value types.
  • Typecasting enables you to check and interpret the type of a class instance at runtime.
  • In class, a Shared mutable state is required, and in struct, unique copies with an independent state are required.
  • In class, Objective-C interoperability is required and in a struct, the data is used in multiple threads.

Class vs Protocol

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.

  • Classes are concrete things. While they might adopt protocols — i.e., 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 typed definitions.
  • Protocols are like abstract definitions, whereas classes and structs are real things you can create.

Structure vs Protocol

  • 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 passed by value from one variable/function to the next.

Thanks!

--

--

Rasalingam Ragul
Rasalingam Ragul

Written by Rasalingam Ragul

Software Engineering Undergraduate

No responses yet