views:

1079

answers:

5

Unless I'm missing something, it seems arbitrary in which .h file I put the protocol definition. I'm even wondering if it could be in it's own .h file... (in Java, it's in its own file)

+2  A: 

I think as long as it is defined somewhere and included in one of the files you are compiling, it is defined. There are no conventions like "ClassName.java" in Objective-C like there is in Java.

This is pretty helpful when you want to do things like categories for things like private methods/properties.

Benny Wong
A: 

In Objective-C is same. Because a protocol is a list of methods that is shared among classes. The protocol is only the list of the methods without corresponding implementations. They are meant to be implemented by someone else.

mperovic
+2  A: 

Protocols are usually defined in their own .h file (in my experience). But they can be defined in a shared.h file. It'd be hard to allow users of the protocol to adopt the protocol in a shared file arrangement and furthermore, it would clutter up your API. Documentation and usage would probably be easier if the protocol is in it's own .h file.

Also, making use of your protocol for things like polymorphic implementations would have less overhead if you just needed to include the protocol .h file instead of a .h file that has other class declarations. I'm not sure on the exact cost/savings of this idea, but I've read there would be some (smaller binary?)

Regards, Frank

Frank V
+3  A: 

I think the placement of the protocol depends on how you're using it. A lot of the time, a protocol is used to define functionality of a delegate or data source for another class. If that's the case, I think you can safely put the protocol definition at the top of the other class - since they are bound to be used together.

If you're defining a protocol in place of a shared base class, you should probably put it in a separate file. For instance, I have several different classes that implement the "Operation" protocol. Some of my other functions expect to receive an object implementing the protocol and don't care too much about the actual classes. In that case, it makes sense to put the protocol definition in it's own header file so you can include it on it's own.

Benny is right, though - it will technically be defined no matter where you put it (as long as it's included somewhere prior to it's use).

Ben Gotow
A: 

It's really pretty much a matter of style in Objective-C. I suppose the really "proper" way is to create a seperate .h file for the protocol, and if I was making a protocol that didn't belong to anything else (like NSCoding for instance) that's just what I would do. On the other hand, most of the time when I create a protocol (or an informal protocol, by using a category on NSObject) it's tied to another class, like NSTableView's data source informal protocol. In these situations, I just put the declaration in that class's header file for simplicity's sake.

Marc Charbonneau