Struct , Proto & Class in Swift :-
Hello guys. Today I am going to give you some idea about struct, proto and class in swift and do a comparison between these concepts.
Class:-
What is a class?
Classes in Swift are building blocks of flexible constructs. Similar to constants, variables and functions the user can define class properties and methods. Swift provides us the functionality that while declaring classes the users need not create interfaces or implementation files. Swift allows us to create classes as a single file and the external interfaces will be created by default once the classes are initialized.
Syntax of a class:-
Class classname {
Definition 1
Definition 2
---
Definition N
}
Class Definition:-
class student {
var studname: String
var mark: Int
var mark2: Int
}
Struct:-
What is a Struct?
You use structs, or “structures”, in Swift to wrap and store complex data types. Let’s dive right in with an example:
struct Receipt
{
var ID:Int = 0
var productName:String = “”
var productPrice:Double = 0.0
var isPaid:Bool = false
}
In the above code, a struct named Receipt is declared. It has 4 properties, such as productName of type String. The above code is similar to how classes are declared in Swift.
You can also create a Receipt object, keeping the initial values as they are, and changing the properties directly. Like this:
var receipt = Receipt()
receipt.productPrice = 2.99
receipt.productName = “Coffee”
Use structures when you want to define a complex data type in Swift. Think of tangible data types, such as Circle, User, Tweet and Article.
Proto:-
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
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
}
Usually, if we use get
, we can make it a const
, var
, let
, or computed property. However, using the property declaration get set
for the salary
property limits var salary: Int { get set }
to var
.
In summary, protocols allow us to group our methods, properties, and functions. However, these protocols can only conform to classes, enums, and structs.
Comparison between Class, Struct and Proto in Swift :-
1)Class vs Struct :-
first let’s do a comparison between class and struct.
- 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.
2) Class vs Proto :-
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.
2) Struct vs Proto :-
following are some diffrences 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.
So I think that you got a clear idea about struct, proto and class in swift by referring this article.