I think that, except for the name, the protocols are much better suited to work as "interfaces" between classes. They do all that the @interface
s do (exposing properties and methods) and on top of that different classes can implement the same protocol which is a huge advantage in tandem with the dynamical nature of Objective-C. So why do we still use @interface
s? What advantages do they bring comparing with protocols? (I hope to get more out of this question than a "they are explicit in what they do" or "closed as not constructive".)
Asked
Active
Viewed 280 times
0

Meda
- 2,776
- 4
- 20
- 31
-
You're asking the difference between OO classes and interfaces (which Obj-C calls interfaces and protocols). This is a very basic OO principles question. – Mike Weller Apr 12 '13 at 12:35
-
You might get the discussion you're looking for at http://programmers.stackexchange.com rather than here. – Phillip Mills Apr 12 '13 at 12:38
-
@MikeWeller If you think that's a duplicate, you didn't read my question. How is a class==interface? – Meda Apr 12 '13 at 12:49
-
1An Obj-C interface is what traditional OO calls a 'class'. An Obj-C protocol is what traditional OO calls an 'interface'. You are therefore asking what the difference is between an OO class and interface and when to use them. – Mike Weller Apr 12 '13 at 12:50
-
Possible duplicate of [Interface and protocol explanation](http://stackoverflow.com/q/1679145) See also: [Differences between Java interfaces and ObjC protocols](http://stackoverflow.com/q/990360) – jscs Apr 12 '13 at 19:14
1 Answers
0
A delegate protocol needs to be defined as such
@protocol
//methods
@end
it can be put in any .h class, you just need to import i t whenever you are going to use it.
A protocol is not like a java interface, a protocol is an adapter that allows two classes to works together. Basically it says, if you want class A to send you messages about its state and actions these are the methods it will call on its delegate that you must implement. Its not like an interface because an interface says if you want to subclass this class you must implement these methods, the protocol says if you want to interact with this class you must implement these methods, so its somewhat different.

Community
- 1
- 1

M.Shuaib Imran
- 1,287
- 16
- 29
-
1I don't see why you link protocols to delegates. Think bigger( polymorphism). – Meda Apr 12 '13 at 12:51