So I\'m trying to wrap my head around Objctive-C interfaces, delegates and protocols. So I have a question:
Does a delegate have to be in a separate file or can it
The point of a delegate is to be notified when another object does something. For example one of your objects wants to know that a window is being closed, so you register it as the window's delegate and implement the windowWillClose:
method. It will be called by NSWindow
appropriately. So the delegate methods are usually defined in another class. Up to a certain point, it lets you extend the functionality of a class without subclassing it.
(Edit: see Daniel's answer about protocols.)
The @interface
is the class declaration, where the member variables and methods are listed. It's located in the .h
, that you import if you need to use the class. The code for methods sits in the @implementation
in the .m
file. In Java it's different, the .java
file serves both purposes.