Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C interfaces, delegates, and protocols

So I'm trying to wrap my head around Objective-C interfaces, delegates and protocols. So I have a question:

Does a delegate have to be in a separate file or can it be methods defined in your class?

Is a protocol like a java interface? This is the way I understand it at the moment where it basically makes you implement methods if you use this protocol.

I'm still confused about interfaces. I'm pretty sure they have no resemblance to what an interface is in java. Maybe it's just a declaration of variables that will be implemented in the class.

like image 816
TheGambler Avatar asked Sep 03 '09 19:09

TheGambler


1 Answers

A delegate protocol needs to be defined as such

@protocol 
//methods
@end

it can be put in any .h class, you just need to import i t whenever you are going to use it.

A protocol is not like a java interface, a protocol is an adapter that allows two classes to works together. Basically it says, if you want class A to send you messages about its state and actions these are the methods it will call on its delegate that you must implement. Its not like an interface because an interface says if you want to subclass this class you must implement these methods, the protocol says if you want to interact with this class you must implement these methods, so its somewhat different.

like image 110
Daniel Avatar answered Oct 26 '22 13:10

Daniel