0

Possible Duplicate:
What is the difference between Notifications, Delegates, and Protocols?

I am new in Objective C. Would like to understand the concept of Protocols and Delegates in a few words. Like under what situation should I consider declaring a Protocol, and then consume it in my classes as delegates or let other classes consume it.

Instead of Protocols why can't I make my classes do the same job?

Thanks for your time.

Community
  • 1
  • 1
XMarshall
  • 953
  • 3
  • 11
  • 23
  • possible duplicate of [What is the difference between Notifications, Delegates, and Protocols?](http://stackoverflow.com/questions/7118598/) also [Difference between delegates and protocols](http://stackoverflow.com/questions/5431413/difference-between-protocol-and-delegates) also [Delegate vs. protocol](http://stackoverflow.com/questions/6361958/delegate-vs-protocol) – jscs Sep 29 '11 at 07:04

1 Answers1

0

Protocols are like contracts, Your class must implement all methods that are @required, @optional are on the other hand not required.

Instead of Protocols why can't I make my classes do the same job? You can, but like I wrote above, its a contract. You know that class

implements the (Required) methods.

delegate is (should be) a weak reference to the class that implements the given protocol.

You are recommended to use the following attribute on your delegateproperty:

@property (assign) id<yourProtocol> delegate;

Notice that you don't retain your delegate. You you (weak) reference because you dont want to get into a retain circle (A retains B, and B retains A).

I hope my explanation helped a bit.

Konrad77
  • 2,515
  • 1
  • 19
  • 36