Structure, Protocol, and Class in Swift

Warunajith Bandara
4 min readAug 13, 2022

--

Swift Language

Swift is a powerful and intuitive programming language for iOS, iPadOS, macOS, tvOS, and watchOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design and produces software that runs lightning-fast.

Swift is the result of the latest research on programming languages, combined with decades of experience building Apple platforms. Named parameters are expressed in a clean syntax that makes APIs in Swift even easier to read and maintain. From its earliest conception, Swift was built to be fast. Using the incredibly high-performance LLVM compiler technology, Swift code is transformed into optimized machine code that gets the most out of modern hardware. You can create an entirely new application with Swift today, or begin using Swift code to implement new features and functionality in your app. Swift code co-exists alongside your existing Objective-C files in the same project, with full access to your Objective-C API, making it easy to adopt.

What is Structure?

Swift structures are the flexible basic building blocks of the programs. The “struct” keyword is used to define structures. By using structures, you can define constructs methods, and properties. Structs are complex data types, meaning that they are made up of multiple values. You then create an instance of the struct and fill in its values, then you can pass it around as a single value in your code. For example, we could define a Person struct type that contains two properties clothes and shoes.

When you define a struct, Swift makes them very easy to create because it automatically generates what’s called a memberwise initializer. In plain speak, it means you create the struct by passing in initial values for its two properties, like this.

Once you have created an instance of a struct, you can read its properties just by writing the name of the struct, a period, then the property you want to read.

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. The protocol just holds the method or properties definition, not their actual body. The protocol must specify whether the property will be gettable or gettable and settable.

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. We use the protocol keyword to define a protocol. For example,

What is a Class?

A class is considered as a blueprint of objects. We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object. Since many houses can be made from the same description, we can create many objects from a class.

We use the class keyword to create a class in Swift. For example,

An object is called an instance of a class. For example, suppose Bike is a class then we can create objects like bike1, bike2, etc from the class.

Here’s the syntax to create an object.

Difference Between Struct, Class, and Protocol

One of the most important differences is that a struct is a value type while a class is a reference type. Although this could be a blog post on its own, a short explanation should be enough to understand this difference. References to a class instance share single data which means that any changes in that class will be available to each reference. In comparison, classes are concrete things. While they might adopt the protocols, 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. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

References:-

--

--

Warunajith Bandara
Warunajith Bandara

Written by Warunajith Bandara

Associate Software Engineer(AI/ML) @ Eutech Cybernetics

No responses yet