What is Class, Struck & Proto in Swift

IOS mobile development

Thushara Samaraweera
3 min readMay 23, 2022

Hi friends, Let’s find out what is Struck, Proto and Class in Swift language. In this article, I am going to give you some idea about struck, proto and class in swift and do a comparison between these concepts.

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.

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

But classes have additional capabilities that structures don’t have:

  • Inheritance enables one class to inherit the characteristics of another.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
  • Reference counting allows more than one reference to a class instance.

Here’s an example of a structure definition and a class definition:

struct HomeAddress{      // example for struct
var no = 0
var road = ""
}
class Student { // example for student
var name: ""
var age: 0
}

Whenever you define a new structure or class, you define a new Swift type. Give types UpperCamelCase names

Structure and Class Instances

The HomeAddress definition and theStudent class definition only describe what a HomeAddressor Studentwill look like. They themselves don’t describe a specific resolution or video mode. To do that, you need to create an instance of the structure or class.

The syntax for creating instances is very similar for both structures and classes:

let studentAddress1 = HomeAddress() studentAddress1.no = 100
studnetAddress1.road = 'high level'
let student1 = Student()
student1.name = "Amal"
studnet1.age = "21"

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

If we want to build a salary remittance system for an app and we have an Employee class, using a protocol looks like the following:

protocol EmployeeProtocol {
var emplname: String { get }
var description: String { get }
var salary: Int { get set }
func paySalary(salary: Int) -> String
}

Now let’s see what are the differences between class and proto in Swift.

  • 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.

following are some differences between structs and protos;

  • 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.

References:

Thank you very much for reading!

Hope to see you again with next article. Till then, STAY SAFE!!!

Happy reading …

-Thushara Samaraweera-

--

--