Friday, September 28, 2012

Categories


Categories

An easy way to add methods to any existing class.

#import "ClassName.h"
@interface ClassName ( CategoryName )
// method declarations
@end

From Documentation:
How You Can Use Categories
There are several ways in which you can use categories:
  • To extend classes defined by other implementorsFor example, you can add methods to the classes defined in the Cocoa frameworks. The added methods are inherited by subclasses and are indistinguishable at runtime from the original methods of the class. 
  • As an alternative to a subclassRather than define a subclass to extend an existing class, through a category you can add methods to the class directly. For example, you could add categories to NSArray and other Cocoa classes. As in the case of a subclass, you don’t need source code for the class you’re extending.
  • To distribute the implementation of a new class into multiple source files For example, you could group the methods of a large class into several categories and put each category in its own file. When used like this, categories can benefit the development process in a number of ways—they:
    • Provide a simple way of grouping related methods. Similar methods defined in different classes can be kept together in the same source file.
    • Simplify the management of a large class when several developers contribute to the class definition.
    • Let you achieve some of the benefits of incremental compilation for a very large class.
    • Can help improve locality of reference for commonly used methods.
    • Enable you to configure a class differently for separate applications, without having to maintain different versions of the same source code.
  • To declare informal protocols See “Informal Protocols ,” as discussed under “Declaring Interfaces for Others to Implement.”
Although the Objective-C language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from doing so. A category is not a substitute for a subclass. There are several significant shortcomings to using a category to override methods:
  • When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that exists in the category's class, there is no way to invoke the original implementation.
  • A category cannot reliably override methods declared in another category of the same class.This issue is of particular significance because many of the Cocoa classes are implemented using categories. A framework-defined method you try to override may itself have been implemented in a category, and so which implementation takes precedence is not defined.
  • The very presence of some category methods may cause behavior changes across all frameworks. For example, if you override the windowWillClose: delegate method in a category on NSObject, all window delegates in your program then respond using the category method; the behavior of all your instances ofNSWindow may change. Categories you add on a framework class may cause mysterious changes in behavior and lead to crashes.

No comments:

Post a Comment