Benefits:
You can extend any class, even those for which you have no source. Look, for example, at the UI extensions Apple added to the NSString class for rendering, getting metrics, etc.
Since you have access to all instance variables, categories provide you with a good way to structure your code through compilation units using logical grouping instead of using the โeverything should be in one fiscal placeโ approach, such as Java.
Disadvantages:
- You cannot safely override methods already defined by the class itself or another category.
AFAIK, languages โโdo not give any 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
source share