About iOS and protocols

iOS has something that called protocols, that has some resemblance to a Java interfaces.

  • Protocols have only functions
  • no members
  • all public (well this is obvious as objective C has no access modifiers)

Declaring a protocol:

@protocol MainControllerProtocol <NSObject>


the @required means that this method has to be implemented in the underlining class.

Assigning protocol:

Unlike Java, assigning protocol is slightly different, as Objective C must get an actual class. BUT, we don’t have one, otherwise there is no point in the protocol! so what to do?

Here is the answer: derive a bogus class from id, like this:

id<MainControllerProtocol> controller;

This will work great. in a function, don’t forget to put this in a brackets.