通知、委托和协议之间有什么区别?

发布于 2024-11-30 15:24:16 字数 54 浏览 0 评论 0原文

协议或委托和 NSNotifications 之间有什么区别?什么是“观察者”以及它如何工作?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-12-07 15:24:16

协议

文档:协议

协议是定义对象响应的某些方法的接口。协议的关键在于它们可以被任何类采用,从而保证对象响应这些方法。

如果你声明一个协议:

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

那么你可以从其他不一定直接相关的类中采用它:

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

或者

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

现在,在其他地方,如果我们只关心协议的一致性,我们可以互换使用这些类中的任何一个:

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

委托和协议通知

文档:Cocoa 设计模式< /a>

这是在对象之间传递消息的两种方法。主要区别:

  • 对于委托,一个指定对象接收消息。
  • 任意数量的对象在发布时都可以收到通知。

委托通常使用协议来实现:类通常具有类似的东西

@property (weak) id<MyCustomDelegate> delegate;

,它为委托提供了一组要实现的特定方法。您可以使用

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

,然后该对象可以向其委托发送相关消息。有关一个很好的常见示例,请参阅 UITableViewDelegate 协议

另一方面,通知是使用 NSNotificationCenter。一个对象(或多个对象)只需将自身添加为特定通知的观察者,然后可以在另一个对象发布通知时接收它们。

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

然后只需实现

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

您就可以使用从任何地方发布通知

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

并确保在完成后调用 removeObserver:

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!

无声情话 2024-12-07 15:24:16

您可以通过在 stackoverflow 中搜索来找到答案...

You can find answers by searching in stackoverflow ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文