What are the difference between a Struct, a proto(protocol), and a class in swift ?

M.R.M Abdullah
3 min readMay 23, 2022

--

Hello Developers…!

Today I like to share Some Knowledge about Structs, a proto(protocol buffer), and class in Swift when it come to IOS Development.

Swift

Swift is the result of cutting-edge programming language research and decades of experience building Apple platforms. Named arguments have a simple syntax that makes Swift APIs considerably easier to learn and manage. Even better, you won’t have to type any semi-colons. Modules eliminate headers and offer namespaces, while inferred types make code clearer and less prone to errors. Strings are Unicode-correct and use a UTF-8-based encoding to enhance efficiency for a wide range of use cases, so they can handle international languages and emoji. Memory is automatically managed via strict, deterministic reference counting, which keeps memory usage to a bare minimum without the burden of garbage collection.

Structures and classes are the building blocks of flexible constructs, helping developers decide how to store data and model behavior in their programs. Classes within Swift are often seen as a blueprint for creating objects.

With the ability to store values by defining properties and adding functionality through creating methods, classes and structs’ shared features can often be used interchangeably in Swift. However, they both have differences and uniqueness, bringing the flexibility to developers to use them where they deem best.

protocol is a blueprint that a class or struct follows, a protocol defines a blueprint of methods or properties that can then be adopted by classes. We may group our methods, attributes, and functions using protocols. These protocols, on the other hand, can only conform to classes and structs.

Comparison between Class, Struct and Proto in Swift

  • protocols in Swift offer communication between unrelated objects where we define the methods and variables observed in classes, enums, and structs.
  • Protocols are effectively like interfaces and Structs are like classes, but they are passed by-value when passing them from one variable/function to another.
  • In their basic form, a protocol/proto 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 like class.
  • 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 type definitions.
  • Protocols are like abstract definitions, whereas classes and structs are real things you can create.
  • Classes can inherit from another class, like you inherit from UIViewController to create your own view controller subclass, but struct can’t.
  • Classes can be deinitialized, i.e. you can invoke a deinit() function before the class is destroyed, but struct can’t.
  • Classes are reference types and structs are value types.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • In class, comparing instance identity is needed by using === and, in struct, Comparing instance data is needed by using == .
  • In class, 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 struct, the data is used in multiple threads.

--

--

M.R.M Abdullah
M.R.M Abdullah

Written by M.R.M Abdullah

Software Engineering undergraduate at University of Kelaniya.