Delegators 真的是 Objective-C 中的类别吗?

发布于 2024-12-09 02:48:20 字数 312 浏览 0 评论 0原文

我正在尝试理解非正式协议,到目前为止我知道非正式协议是一种类别,但是,为什么委托者没有类别名称?

示例:

#import <Cocoa/Cocoa.h>

@class FileSystemNode;

@interface AppController : NSObject { // Where is the Category ????  
@private
    IBOutlet NSBrowser *_browser;
    FileSystemNode *_rootNode;
}

@end

谢谢。

I'm trying to understand the Informal protocols, until now I know that Informal protocols are a kind of Category, but, why delegators don't have a category name?

Example:

#import <Cocoa/Cocoa.h>

@class FileSystemNode;

@interface AppController : NSObject { // Where is the Category ????  
@private
    IBOutlet NSBrowser *_browser;
    FileSystemNode *_rootNode;
}

@end

Thanks.

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

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

发布评论

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

评论(1

惯饮孤独 2024-12-16 02:48:20

非正式协议不是“一种类别”。

如果类符合协议(例如 @interface MyClass),则可以保证您可以调用该类上 MyProtocol 所需的所有方法。 (除非类没有真正实现它们[链接器无法知道,因为动态绑定],这是一个编程错误和谎言,但通常情况并非如此。)

另一方面,类别允许您获取一个已经存在的类并使用新方法扩展它(例如向现有的 NSView 类添加一个 removeAllSubviews 方法)。


因此,类别和协议是完全相反的。

Informal protocols are not "a kind of category".

If a class conforms to a protocol (for example @interface MyClass <MyProtocol>) it is guaranteed that you can call all methods on that class that are required by MyProtocol. (Unless the class doesn't really implement them [the linker can't know that because of dynamic binding], which is a programming error and a lie, but that is usually not the case.)

Categories on the other hand allow you to take an already existing class and extend it with new methods (for example adding a removeAllSubviews method to the already existing NSView class).


So, categories and protocols are quite the opposite of each other.

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