Protocol in swift
Blueprint for your operation
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.
Protocol syntax
placing the protocol’s name after the type’s name, separated by a colon, as part of their definition.
Multiple protocols can be listed, and are separated by commas
Protocol used in structure Syntax
Protocol used in class syntax
If a class has a superclass, list the superclass name before any protocols it adopts, followed by a comma
Protocol Requirments
Protocol is used to specify particular class type property or instance property. It just specifies the type or instance property alone rather than specifying whether it is a stored or computed property. Also, it is used to specify whether the property is ‘gettable’ or ‘gettable and settable’.
Structure Example
Here’s an example of a simple structure that adopts and conforms to the Identifiable
protocol
In the above program, the structure adopts the Identifiable
protocol as part of the first two lines of its definition.
Class example
Swift reports an error at compile-time if a protocol requirement is not fulfilled
Method requirement
These methods are written as part of the protocol’s definition in the same way as for normal instance and type methods, but without curly braces or a method body. Method parameters can’t be specified within a protocol’s definition.
Syntax
Example
Here’s an implementation of a class that adopts and conforms to the Identifiable
protocol.
Mutating methods
If you define a protocol instance method requirement that is intended to mutate instances of any type that adopts the protocol, mark the method with the mutating
keyword as part of the protocol’s definition. This enables structures and enumerations to adopt the protocol and satisfy that method requirement.
you don’t need to write the
mutating
keyword when writing an implementation of that method for a class.This keywords is only used by structures and enumerations.
Example
Initializer Requirements
Protocols can require specific initializers to be implemented by conforming types. You write these initializers as part of the protocol’s definition in the same way as for normal initializers, but without curly braces or an initializer body.
Syntax
Class Implementations of Protocol Initializer Requirements
Syntax
The
required
modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer
The use of the required
modifier ensures that you provide an explicit or inherited implementation of the initializer requirement on all subclasses of the conforming class, such that they also conform to the protocol.
Example
Protocol Inheritance
A protocol can inherit one or more other protocols and can add further requirements on top of the requirements it inherits.
The syntax for protocol inheritance is similar to the syntax for class inheritance, but with the option to list multiple inherited protocols, separated by commas
Example
In the above program, a new protocol, StudentDetails
, which inherits from TotalMark
and Percentage
protocols. Now class, structure, and enumeration adopts StudentDetails
protocol must satisfy all of the requirements enforced by TotalMark
and Percentage
, plus the additional requirements enforced by StudentDetails
.
When you use StudentDetails
protocol in your class, Structure or enumeration you must be include calculatingTotalMark() , calculatingPercentage() and printingDetails()
this functions.
Class-Only Protocols
You can limit protocol adoption to class types by adding the AnyObject
protocol to a protocol’s inheritance list.
In the above example, StudentDetails
protocol can only be adopted by class types.
If a non -class type tries to conform to this protocol, the compiler throws an error.
Protocol Composition
It can be useful to require a type to conform to multiple protocols at the same time. You can combine multiple protocols into a single requirement with a protocol composition.
Protocol compositions behave as if you defined a temporary local protocol that has the combined requirements of all protocols in the composition. Protocol compositions don’t define any new protocol types.
You can list as many protocols as you need, separating them with ampersands (
&
).
Example
In the above program, the StudentName
protocol has a single requirement for a gettable and settable String
property. The StudentAge
protocol has a single requirement for a gettable and settableString
property.
Both protocols are adopted by a structure called StudentDetails
. In structure-function defines printingDetails(details:)
. The type of the details
parameter is StudentName & StudentAge .
which means “any type that conforms to both the Named
and Aged
protocols”.
First, create a new StudentDetails
an instance for student
and passes this new instance to the printingDetails
function. Because StudentDetals
conforms to both protocols, this call is valid, so printingDetails()
function can print name and age .
Protocol Extension
Protocols can be extended to provide method, initializer, subscript, and computed property implementations to conforming types.
Protocols let you describe what methods something should have, but don’t provide the code inside. Extensions let you provide the code inside your methods, but only affect one data type — you can’t add the method to lots of types at the same time.
Example
In the above program, the StudentDetails
protocol can be extended to be provide age()
method , which uses to return a string value. By creating an extension on the protocol, all conforming types automatically gain this method implementation without any additional modification.
Protocol extensions can add implementations to conforming types but can’t make a protocol extend or inherit from another protocol.
Collection extension
In swift, array and sets both conform to a protocol Collection()
. so we can write an extension to that protocol to add a method called summarize()
to print an array of elements.