Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Protocol?

Tags:

objective-c

I've read the documentation but I'm still confused. Can someone please explain what a protocol is? (You could give code examples but I'm really looking for an explanation)

like image 342
Defender Of The Code Avatar asked Oct 20 '10 20:10

Defender Of The Code


People also ask

What are protocols simple definition?

The most common meaning of protocol is “a system of rules that explain the correct conduct and procedures to be followed in formal situations,” as in these example sentences: The soldier's actions constituted a breach of military protocol. They did not follow the proper diplomatic protocols.

What are protocols in computer?

A network protocol is an established set of rules that determine how data is transmitted between different devices in the same network. Essentially, it allows connected devices to communicate with each other, regardless of any differences in their internal processes, structure or design.

What is protocol with example?

Protocols: It is a set of rules that need to be followed by the communicating parties in order to have successful and reliable data communication. For example - Ethernet and HTTP.

What is a protocol and why is it needed?

Protocols provide us with a medium and set of rules to establish communication between different devices for the exchange of data and other services. Protocols are needed in every field like society, science & technology, Data Communication, media, etc.


2 Answers

Here's a great article on it. Effectively, a protocol in Objective-C is very similar to an interface in Java or a pure virtual class in C++ (although not exactly as pure virtual classes can have data members...). It's basically a guarantee that a specific class knows how to respond to a given set of methods (messages).

Edit The original article disappeared so I have replaced it with a different tutorial.

like image 172
Chris Thompson Avatar answered Nov 15 '22 13:11

Chris Thompson


A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts. Cocoa uses protocols to support interprocess communication through Objective-C messages. In addition, since Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.

A good example of a protocol is NSCoding, which has two required methods that a class must implement. This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.

   @protocol NSCoding

     -(void)encodeWithCoder:(NSCoder *)aCoder;

     -(id)initWithCoder:(NSCoder *)aDecoder;

   @end

To adopt a protocol, enclose the name of the protocol in <> like below

   @interface SomeClass : NSObject <NSCoding> 

    {
     some variables
    }

How to define a Protocol?

We can create both required an optional methods within a protocol. What follows is a definion of a protocol named 'Hello':

   @protocol Hello
    - (BOOL)send:(id)data;
    - (id)received;
   @optional
    - (int)progress;
   @end

To use the protocol, as with the example above, declare the protocol in the interface and write the required methods in the class implementation:

// Interface @interface AnotherClass : NSObject

   {
    some declaration
   }

// Implementation @implementation AnotherClass

  - (BOOL)send:(id)data
   {
    some declaration
   }

  - (id)received
   {
    some code
   }

// Optional method

  - (int)progress
   {
    some code 
   }
   @end

I hope it helps you to learn Protocol.

like image 20
Gajendra K Chauhan Avatar answered Nov 15 '22 14:11

Gajendra K Chauhan