|
|
|
|
|
|
|
| PATH |

You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class.
A category can be an alternative to a subclass. Rather 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.
The methods the category adds become part of the class type. For example, methods added to the NSArray class in a category will be among the methods the compiler will expect an NSArray instance to have in its repertoire. Methods added to the NSArray class in a subclass would not be included in the NSArray type. (This matters only for statically typed objects, since static typing is the only way the compiler can know an object's class.)
Category methods can do anything that methods defined in the class proper can do. At run time, there's no difference. The methods the category adds to the class are inherited by all the class's subclasses, just like other methods.
The declaration of a category interface looks very much like a class interface declaration-except the category name is listed within parentheses after the class name and the superclass isn't mentioned. Unless its methods don't access any instance variables of the class, the category must import the interface file for the class it extends:
#import "ClassName.h" @interface ClassName ( CategoryName ) method declarations @end
The implementation, as usual, imports its own interface. Assuming that the interface file is named after the category, a category implementation looks like this:
#import "CategoryName.h" @implementation ClassName ( CategoryName ) method definitions @end
Note that a category can't declare any new instance variables
for the class; it includes only methods. However, all instance variables
within the scope of the class are also within the scope of the category.
That includes all instance variables declared by the class, even
ones declared @private.
There's no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.
The methods added in a category can be used to extend the
functionality of the class or override methods the class inherits.
A category can also override methods declared in the class interface.
However, it cannot reliably override methods declared in another
category of the same class. A category is not a substitute for a
subclass. It's best if categories don't attempt to redefine
methods that are explicitly declared in the class's @interface section. Also
note that a class can't define the same method more than once.
Note: When
a category overrides an inherited method, the new version can, as
usual, incorporate the inherited version through a message to super.
But there's no way for a category method to incorporate a method
with the same name defined for the same class. |
Categories can be used to extend classes defined by other implementors-for example, you can add methods to the classes defined in the Cocoa frameworks. The added methods will be inherited by subclasses and will be indistinguishable at run time from the original methods of the class.
Categories can also be used to distribute the implementation of a new class into separate source files-for example, you could group the methods of a large class into several categories and put each category in a different file. When used like this, categories can benefit the development process in a number of ways:
Categories are also used to declare informal protocols, as discussed under "Protocols" .
A category can add methods to any class, including the root class. Methods added to NSObject become available to all classes that are linked to your code. While this can be useful at times, it can also be quite dangerous. Although it may seem that the modifications the category makes are well understood and of limited impact, inheritance gives them a wide scope. You may be making unintended changes to unseen classes; you may not know all the consequences of what you're doing. Moreover, others who are unaware of your changes won't understand what they're doing.
In addition, there are two other considerations to keep in mind when implementing methods for the root class:
super are invalid
(there is no superclass).Normally, class objects can perform only class methods. But instance methods defined in the root class are a special case. They define an interface to the run-time system that all objects inherit. Class objects are full-fledged objects and need to share the same interface.
This feature means that you need to take into account the
possibility that an instance method you define in a category of
the NSObject class might be performed not only by instances but
by class objects as well. For example, within the body of the method, self might
mean a class object as well as an instance. See the NSObject class
specification in the Foundation Framework reference for more information
on class access to root instance methods.

© 2001 Apple Computer, Inc.
|
|
|
Contact Us | Privacy Notice Copyright © 2000 Apple Computer, Inc. All rights reserved. 1-800-MY-APPLE |