Delegators 真的是 Objective-C 中的类别吗?
我正在尝试理解非正式协议,到目前为止我知道非正式协议是一种类别,但是,为什么委托者没有类别名称?
示例:
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非正式协议不是“一种类别”。
如果类符合协议(例如
@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 byMyProtocol
. (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 existingNSView
class).So, categories and protocols are quite the opposite of each other.