开发者

Is Delegators really Categories in Objective-C?

开发者 https://www.devze.com 2023-04-11 11:34 出处:网络
I\'m trying to understand the Informal protocols, until now I know th开发者_开发技巧at Informal protocols are a kind of Category, but, why delegators don\'t have a category name?

I'm trying to understand the Informal protocols, until now I know th开发者_开发技巧at 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消