"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;
@end;
In DemoCategory.m:
@implementation HypotheticalClass (DemoCategory) {
    - thing1 {
        puts("I AM UNUSABLE SAMPLE CODE.");
    }
    - thing2 {
        puts("THERE ARE SPIDERS BITING ME");
    }
}
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.

Cat"e*go*ry (?), n.; pl. Categories (#) [L. categoria, Gr. , fr. to accuse, affirm, predicate; down, against + to harrangue, assert, fr. assembly.]

1. Logic. One of the highest classes to which the objects of knowledge or thought can be reduced, and by which they can be arranged in a system; an ultimate or undecomposable conception; a predicament.

The categories or predicaments -- the former a Greek word, the latter its literal translation in the Latin language -- were intended by Aristotle and his followers as an enumeration of all things capable of being named; an enumeration by the summa genera i.e., the most extensive classes into which things could be distributed. J. S. Mill.

2. Class; also, state, condition, or predicament; as, we are both in the same category.

There is in modern literature a whole class of writers standing within the same category. De Quincey.

 

© Webster 1913.

Log in or registerto write something here or to contact authors.