The necessity for protocols are to abstract the methods of classes which are not hierarchically related.
The similar things also can be done with the help a class (interface) which encompasses all those methods and subclass them ? (This is not really possible due to the Multiple inheritance problem since a class has to be derived already from NSObject.ignore the NSProxy case)
What special things that protocols can do than a class?
Are protocols trying to solve only the multiple inheritance problem?
Protocols main advantage is, that they describe what a object should be able to do, without enforcing subclassing. In languages that dont have multiple inheritance such a mechanism is needed, if you want others programmers be able to use your classes. (see delegation)
For an instance Java has something similar, called interfaces.
This means a huge advantage, as it is very easy to build dynamic systems, as I can allow other developers to enhance my classes via a clearly defined protocol.
A practical example:
I am just designing a REST API and I am providing a Objective-C client library.
As my api requires information about the user, I add a protocol
Anywhere I need this user information, I will have an basic id-object, that must conform to this protocol
You can read this line as: “I don’t care, what kind of object you provide here, as long as it knows about
lastName,firstNameanduuid“. So I have no idea, how the rest of that object looks like — and I don’t care.As the library author I can use this safely:
BTW: I wouldn’t call the absence of multi-inheritance a problem. It is just another design.