What is the difference between a method and a protocol
They 开发者_开发知识库say that a protocol is a method, but it's different from a method. So exactly, what does it do and what is the difference between a method and a protocol?
A protocol is most definitely NOT a method! A protocol is a set of methods that a class implements when conforming to that protocol. It is similar to a Java interface. See this informative discussion: Objective-C versus Java Interface
In Objective-C each class has an interface, a list of method defenitions which that class implements, and an implementation, the source for each of those method defenitions.
A protocol is a list of method definitions. A class can support a protocol by implementing the methods it defines.
A protocol is a set of methods that objects of a class may implement.
Let's say you have a protocol that consists of a method A and a method B, then an object conforms to that protocol if it implements method A and method B.
It's also possible for a protocol to include optional methods which are not required to be implemented.
精彩评论