Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c protocol defined in its own .h file?

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)

like image 248
bedouger Avatar asked Jun 05 '09 21:06

bedouger


People also ask

What is a protocol in Objective-C?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.

What is a header file Objective-C?

Header Files in Objective-C. The header file (with the file extension . h) defines the class and everything about it for the world to know. The idea is to separate the definition of a class from its nitty gritty implementation details. In this case, the header file acts as the interface to the rest of your program.


1 Answers

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).

like image 168
Ben Gotow Avatar answered Sep 21 '22 17:09

Ben Gotow