开发者

what are the advantages & disadvantages of using categories ? Why & When we need them?

what are the advantages & disadvantages of using categories ? Why & When w开发者_如何学运维e need them ?


Advantages:

  • You can extend any class, even those, for which you do not have the source. Look, for example, into the UI extensions added by Apple to the NSString class for rendering, getting the metrics, etc.

  • Since you have access to all instance variables, categories provide you with a nice way to structure your code across compilation units using logical grouping instead of the "it must all be in one phyiscal place" approach taken, for example, by Java.

Disadvantages:

  • You cannot safely override methods already defined by the class itself or another category.

AFAIK, the languages makes no guarantees as to which implementation will actually be called if you try something like:

@interface Foo { }
- (void) method;
@end

@interface Foo (MyCategory)
- (void) method;
@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜