Structure, Protocol, and Class in Swift

Tharushi Chamalsha
3 min readMay 24, 2022

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

What is Swift?

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Swift was developed as a replacement for Apple’s earlier programming language Objective-C, as Objective-C had been largely unchanged since the early 1980s and lacked modern language features.

Above mentioned Classes, Structs, and Proto are some key terms in Swift.

Difference between a Struct, a proto, and a class in swift?

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.

Lets get back to them one by one..

What is a class?

Classes in Swift 4 are building blocks of flexible constructs. Similar to constants, variables and functions the user can define class properties and methods. Swift 4 provides us the functionality that while declaring classes the users need not create interfaces or implementation files. Swift 4 allows us to create classes as a single file and the external interfaces will be created by default once the classes are initialized.

Benefits of having Classes:
Inheritance acquires the properties of one class to another class

Type casting enables the user to check class type at run time

Deinitializers take care of releasing memory resources

Reference counting allows the class instance to have more than one reference

What is Structure?

In Swift, a struct is used to store variables of different data types. It can also be seen as a template definition of an object. It can contain

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

What is a protocol?

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

In addition to specifying requirements that conforming types must implement, you can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can take advantage of.

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.

Comparing Structures and Classes

Structures and classes in Swift have many things in common. Both can:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind

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.

In a more advanced form, you can write extensions on your protocols that provide default implementations of the methods. You still can’t provide storage for properties, however.

In comparison, 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.

Hope to see you from another article...

--

--