ProtocolsProtocols are interfaces which define certain methods that objects respond to. The key thing about protocols is that they can be adopted by any class, guaranteeing that an object responds to those methods.If you declare a protocol:
Then you can adopt it from other classes which are not necessarily directly related:
or
Now, elsewhere, we can use any of these classes interchangeably, if we only care about conformance to the protocol:
Delegates & NotificationsDocumentation: Cocoa Design PatternsThese are two ways to pass messages between objects. The main difference:
which gives the delegate a certain set of methods to implement. You can use
and then the object can send relevant messages to its delegate. For a good common example, see the UITableViewDelegate protocol.Notifications, on the other hand, are implemented using NSNotificationCenter. An object (or more than one object) simply adds itself as an observer for specific notifications, and then can receive them when they are posted by another object.
Then just implement
And you can post notifications from anywhere using
|
Featured post
Difference between protocol and delegates?
A protocol, declared with the ( @protocol syntax in Objective-C) is used to declare a set of methods that a class "adopts" (de...
Friday, 17 April 2015
What is the difference between Notifications, Delegates, and Protocols?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment