Table of Contents Previous Section

Categories

A category is a set of methods you add to an existing class. You can add a category to any custom or WebObjects-provided Objective-C class. Because 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. That is, you don't have to instantiate a special subclass.

To create a category, you must implement it within an @implementation block, which is terminated by the @end directive. Place the category name in parentheses after the class name.

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 {
		NSString *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. Here's an example:

	- takeValuesFromRequest:request inContext:context {
		[super takeValuesFromRequest:request inContext:context];
		[self logWithFormat:@"Email address of sender: %@", 
			[request emailAddressOfSender]];
	}
The category must be included either at the end of a component's script file (that is, a script file within a .wo) or it must be included in a scripted class's stand-alone script file. Do not place categories in the application or session script.

Table of Contents Next Section