Table of Contents Previous Section

Categories

Categories are a feature of WebScript borrowed from Objective-C. They allow you to add methods to an existing class without having to create a subclass of that class. The existing class can be a WebObjects public class or any custom or NeXT-provided Objective-C class. The methods added by the category become part of the class type. You can invoke them on any object of that type within an application.

To create a category you must implement it within an @implementation block, which is terminated by the @end directive. The category name appears in parentheses after the class name. Unlike Objective-C categories, no typing of method arguments or return values is allowed. The category can be in any script file of the application.

The following example is a simple category of WORequest that gets the sender's Internet e-mail address from the request headers ("From" key) and returns it (or "None").

@implementation WORequest(RequestUtilities)
- emailAddressOfSender {
    id address = [self headerForKey:@"From"];
    if (!address) address = @"None";
    return address;
}
@end

Elsewhere in your WebScript code, you invoke this method on WORequest objects just as you do with any other method of that class:

- takeValuesFromRequest:request inContext:context {
    [super takeValuesFromRequest:request inContext:context];
    [self logWithFormat:@"Email address of sender: %@", 
        [request emailAddressOfSender]];
}

Table of Contents Next Section