Protocols are powerful functionality provided by iOS. These are set of behavior that is expected from an object to behave/respond in a given situation.
Protocols are set of methods and properties. One can define protocol as:
// methods and properties
Types of Protocols
There are two kinds of protocols
1- Required: These are mandatory and should be used when a class is adopting a protocol. By default all protocols are required
one can use @required to make methods required/mandatory
2- Optional: These are not mandatory, If a class implements protocol then optional protocols are not mandatory to implement. One can make methods optional using @optional.
We must check before calling optional protocols as below:
// This means class is implementing the optional protocol method.
// code here.
A class can adopt a protocol using angle<> brackets. Here is the syntax how one should conform to a protocol:
@interface ABClass: NSObject
This means that any instance of ABClass will respond not only to the methods declared specifically in the interface, but ABClass also provides implementations for the required methods in MyDefinedProtocol. One need not to to re-declare the protocol methods in the ABClass class interface because the adoption of the MYDefineProtocol is sufficient.