display | more...
"Categories" are, along with protocols (see my writeup under Protocol), how Objective C handles mix-ins and how it gets around the fact it does not have multiple inheritance. Protocols and categories encompass one of the more visible bits of Objective C's Smalltalk heritage, and are taken together really rather similar to a more nimble (in my opinion, anyway) equivalent of the interface )("implements") capability in java. Categories begin to feel a bit clumsy when used extensively, but some would claim you are far better off using them than you are using multiple inheritance.

Categories are specified separately from any existing class, usually in independent .h and .m files. If we were going to make a category named DemoCategory that extended a class named HypotheticalClass by defining two methods named thing1 and thing2, the syntax would look like this:

In DemoCategory.h:

@interface HypotheticalClass (DemoCategory)
    - thing1;
    - thing2;
In DemoCategory.m:
@implementation HypotheticalClass (DemoCategory) {
    - thing1 {
        puts("I AM UNUSABLE SAMPLE CODE.");
    - thing2 {
When the program is run, no distinction whatosever is made between regular methods and Category-inserted methods. Category methods can access object variables natively, and are inherited the same by subclasses. So, categories can be used to let you organize and split up individual objects across multiple files, or to define a group of methods that may be inserted into more than one class in the same way, or if you for some reason want to write several different variations on how a certain group of methods is implemented and easily switch between the variations. Also, because categories are compiled separately from their home classes, you can use a category to modify the workings of a class that you do not have access to the source code of.

The really interesting thing (although this is not often done) is that Categories, once compiled, can be arbitrarily applied to and removed from a given class while the program is running. See Apple's documentation on NSBundle.

Alternately, see Category Theory.