Categories – Apple WebObjects 3.5 User Manual

Page 182

Advertising
background image

Chapter 10

The WebScript Language

182

@interface Surfshop:NSObject {

id name;
NSArray *employees;

}
@end

@implementation Surfshop
- (Surfshop *)initWithName:aName employees:theEmployees {

name = [aName copy];
employees = [theEmployees retain];
return self;

}
@end

Do not use separate files for the

@interface

and

@implementation

blocks. They must

both be in the same file.

To use the class, you locate it in the application, load it, and then allocate and
initialize instances using the class object. Here’s an example:

NSMutableArray *allSurfshops;
- init {

id scriptPath;
id surfshopClass;

[super init];
scriptPath = [[[self application] resourceManager]

pathForResourceNamed:@"Surfshop.wos" inFramework:nil];

surfshopClass = [[self application]

scriptedClassWithPath:scriptPath];

allSurfshops = [NSMutableArray array];
[allSurfshops addObject:[[[surfshopClass alloc] initWithName:

"Banana Surfshop" employees:@("John Popp", "Jenna de Rosnay")]
autorelease]];

[allSurfshops addObject:[[[surfshopClass alloc] initWithName:

"Rad Swell" employees:@("Robby Naish", "Nathalie Simon")]
autorelease]];

return self;

}

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.

Advertising