
Chapter 10 The WebScript Language
170
For more information on NSNumber, NSString, NSDictionary, and NSArray,
see the chapter “WebScript Programmer’s Quick Reference to Foundation
Classes” (page 187).
Methods
To define a new method, simply put its implementation in the script file. You
don’t need to declare it ahead of time. For example, this is the definition of a
method from the Main component in the Visitors example:
- recordMe {
if ([aName length]) {
[[self application] setLastVisitor:aName];
[self setAName:@""]; // clear the text field
}
}
Methods can take arguments. To define a method that takes arguments, you
place the argument name after a colon (:). For example, the following method
takes two arguments. It adds the two arguments together and returns the result:
- addFirstValue:firstValue toSecondValue:secondValue {
id result;
result = firstValue + secondValue;
return result;
}
The strings that appear to the left of the colons are part of the method name.
The method above is named
addFirstValue:toSecondValue:. It takes two arguments,
which it calls
firstValue and secondValue.
If you want, you can add type information for the return values and parameter
values. For example, the following method,
subtractFirstValue:fromSecondValue:,
subtracts one number from another and returns the result:
- (NSNumber *)subtractFirstValue:(NSNumber *)firstValue
fromSecondValue:(NSNumber *)secondValue {
NSNumber *result;
result = secondValue - firstValue;
return result;
}
In these examples, note the following:
•Type information is optional. When there is no type,
id is assumed.
• Explicitly specifying the
id type is not allowed:
// NO!! This won’t work.
- (id)aMethod:(id)anArg { ... }
Komentarze do niniejszej Instrukcji