Protocols & delegates [duplicate]
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.
Instea开发者_高级运维d of Protocols
why can't I make my classes do the same job?
Thanks for your time.
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.
精彩评论