Why would you want a class to conform to a protocol privately?
Asked Answered
P

4

5

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?

Pindling answered 22/11, 2011 at 20:40 Comment(0)
O
3

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.

Oodles answered 22/11, 2011 at 20:45 Comment(1)
I see - so because AppDelegate sets the delegate for the instance of SetupViewController to be self (line 211 of AppDelegate.m) it needs to conform to the protocol. But because no other class should be able to use AppDelegate in this way, this conformance should remain private. Thanks for your answer!Pindling
F
5

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.

Figment answered 22/11, 2011 at 20:47 Comment(0)
O
3

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.

Oodles answered 22/11, 2011 at 20:45 Comment(1)
I see - so because AppDelegate sets the delegate for the instance of SetupViewController to be self (line 211 of AppDelegate.m) it needs to conform to the protocol. But because no other class should be able to use AppDelegate in this way, this conformance should remain private. Thanks for your answer!Pindling
M
1

There are times where you want to be a delegate for another object, but in so doing you may get compiler warnings because you aren't explicitly declaring that your class conforms to the required methods of the protocol. As others have mentioned, one of the pillars of Object Oriented programming is information hiding. It is not desirable to declare in your header that a class implements a particular protocol because you would be breaking that principle. It also opens your class to abuse or to be used in ways it was not intended because it is making that information known to other classes. By declaring a private category in the .m file and letting the compiler know of your intention to implement this protocol, you not only get rid of the warnings that may crop up, but you are, in effect, making your code self-documenting.

Mcavoy answered 22/11, 2011 at 21:36 Comment(0)
C
0

Maybe because you don't want anybody to know about your protocol with except yourself. So no somebody externally of AppDelegate will pass instance of Appdelegate as delegate to another class instance. So you will able to pass it as this internally.

Cohin answered 22/11, 2011 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.