Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you want a class to conform to a protocol privately?

I've been looking at Apple's MVCNetworking example project and part of the interface definition for AppDelegate is puzzling me. In the .h file we have this:

@interface AppDelegate : NSObject
{
  ...

But in the .m file we have this:

@interface AppDelegate () <SetupViewControllerDelegate>
  ...

So this class is privately conforming to the protocol. But why would you want to do this instead of publicly declaring it in the header?

like image 232
David Jones - iPushPull Avatar asked Nov 22 '11 20:11

David Jones - iPushPull


2 Answers

In general, you should publicly expose as little as possible. The fact that the AppDelegate can be a SetupViewController's delegate is probably used when the AppDelegate presents a SetupViewController. No other class should be setting the AppDelegate as some other SetupViewController's delegate, so it wouldn't make sense to publicly advertise that conformance.

like image 164
BJ Homer Avatar answered Sep 28 '22 15:09

BJ Homer


It looks like the implementation uses a SetupViewController internally in one of its "private" methods presentSetupViewControllerAnimated:. Since the view controller is not publicly accessible (through a property or otherwise), there's no need to declare the class as conforming to the protocol from the public point of view. In other words, the protocol is related only to the implementation of the class, and not to the public interface that it presents.

like image 37
Sean Avatar answered Sep 28 '22 16:09

Sean