Structure, Protocol, and Class in Swift Language

In this article I am going to 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.

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.

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.

Pass by Reference VS Pass by Value

Pass-by-reference, each instance of a reference type that is copied shares the data. Although the reference is copied, the data it refers to is not. All instances changes when the other is changed. This is because the memory address is passed to that function.

Pass-by-value involves copying everything of an instance to a different memory address location. A different copy of the data is kept in each instance. It only has access to that one instance when updating or accessing it. As a result, the values of the other instances are unaffected. Only that one instance is affected by the changes.

Difference between a Protocol and a Class/Struct

Swift is protocol-oriented programming langue and even Apple encourages programmers to use protocol than class. Classes and structures are concrete objects in contrast to protocols, which are more of an abstract definition.

Perhaps put more simply:

  • “class” defines what an object is.
  • “protocol” defines a behavior the object has.

When should you use a Struct?

On the other hand, it’s recommended to use Structs in these scenarios:

  • Use structs for simple data types.
  • In a multi-threaded environment, for instance, with a database connection that’s opened in a different thread, structs are safer.
  • When the properties are mostly value types too.
  • When you don’t need Inheritance.

Thank You

— Sandun Rangana Jayasekara —

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sandun jayasekara

Sandun jayasekara

Software Engineer || Undergraduate , University Of Kelaniya , Srilanka