Comparison of Struct , Proto & Class in Swift

Sachithra_Manamperi
3 min readMay 23, 2022

What is a class in Swift?

Swift classes are the basis of flexible constructs. Class attributes and methods can be defined similarly to constants, variables, and functions. Swift has the ability to eliminate the need for users to generate interfaces or implementation files when declaring classes. Swift allows us to create classes in a single file, with external interfaces automatically generated once the classes are initialized.

Example of a Swift Class

Here's how to use the class

What is a Struct in Swift?

A struct is used to store variables of different data types in Swift. For example, suppose we want to save a person’s name and age. We can store value in two variables: name and age. Consider the case where we need to save the same information for multiple people.

The syntax for defining struct in Swift is as follows:

Example for a Structure

A struct named Person has been defined here. The struct has two variables with the values “” and 0 inside the curly braces: name and age.
Properties are the variables and constants that form a struct.

A struct definition is just a blueprint. We must first build an instance of a struct so that we can use it. Here's how to use Struct

Also We can Create Multiple Instances of Struct

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

A protocol is a set of methods or attributes that can be used by classes to implement (or any other types).

To define a protocol, we use the protocol keyword. For example,

The protocol only contains the definition of the method or property, not the actual body.

The property must be gettable or gettable and settable, according specified in the protocol.

Here's one example for swift protocol

If we use get, we can usually turn it into a const, var, let, or calculated property.

All you have to do is pass the. proto file to the protocol buffer compiler, which will build data access classes (structs in Swift) in the supported languages of your choosing. The classes/structs developed in the project you’re working on can then be used.

Comparison between Class, Struct and Proto in Swift

1)Struct vs. Class:

  • Classes can inherit from other classes, such as UIViewController, to construct their own view controller subclasses, but structs cannot.
  • Classes can be deinitialized before they are destroyed by calling the deinit() function, but structs cannot.
  • Structs are value types, while classes are reference types.
  • At runtime, type casting allows you to inspect and interpret the type of a class instance.
  • Comparing instance identity in a class is done with ===, and comparing instance data in a struct is done with ==.
  • Shared mutable state is necessary in classes, while unique copies with independent states are required in structs.
  • Interoperability with Objective-C is necessary in classes, and data is used in many threads in structs.

2) Prototype vs. Class:
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 properties, 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 can be used to generate objects, whereas protocols are simply type declarations.
Classes and structs are real entities you can construct, but protocols are abstract specifications.

3) Prototype vs. Structure:

Here are some differences between structs and protos:

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.
So, after reading this post, I believe you have a good understanding of struck, proto, and class in Swift.

--

--

Sachithra_Manamperi

Undergraduate | Software Engineering | Dharmaraja College Kandy | Sri Lankan