What is the difference between Notifications, Dele

2019-01-11 08:43发布

What is the difference between Protocols or Delegates and NSNotifications? What is an "Observer" and how does it work?

2条回答
叼着烟拽天下
2楼-- · 2019-01-11 08:59

Protocols

Documentation: Protocols

Protocols 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:

@protocol Photosynthesis
@required
- (void)makeFood:(id<Light>)lightSource;
@optional
+ (NSColor *)color; // usually green
@end

Then you can adopt it from other classes which are not necessarily directly related:

@interface Plant : Eukaryote <Photosynthesis>
// plant methods...
@end
@implementation Plant
// now we must implement -makeFood:, and we may implement +color
@end

or

@interface Cyanobacterium : Bacterium <Photosynthesis>
// cyanobacterium methods...
@end
@implementation Cyanobacterium
// now we must implement -makeFood:, and we may implement +color
@end

Now, elsewhere, we can use any of these classes interchangeably, if we only care about conformance to the protocol:

id<Photosynthesis> thing = getPhotoautotroph(); // can return any object, as long as it conforms to the Photosynthesis protocol
[thing makeFood:[[Planet currentPlanet] sun]]; // this is now legal

Delegates & Notifications

Documentation: Cocoa Design Patterns

These are two ways to pass messages between objects. The main difference:

  • with delegates, one designated object receives a message.
  • any number of objects can receive notifications when they are posted.

Delegates are usually implemented using protocols: a class will usually have something like

@property (weak) id<MyCustomDelegate> delegate;

which gives the delegate a certain set of methods to implement. You can use

myObject.delegate = /* some object conforming to MyCustomDelegate */;

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.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(notificationHappened:)
                                             name:MyCustomNotificationName
                                           object:nil];

Then just implement

- (void)notificationHappened:(NSNotification *)notification {
    // do work here
}

And you can post notifications from anywhere using

[[NSNotificationCenter defaultCenter] postNotificationName:MyCustomNotificationName
                                                    object:self
                                                  userInfo:nil];

And make sure to call removeObserver: when you're done!

查看更多
别忘想泡老子
3楼-- · 2019-01-11 09:00

You can find answers by searching in stackoverflow ...

查看更多
登录 后发表回答