Apple WebObjects 3.5 User Manual

Page 171

Advertising
background image

WebScript Language Elements

171

A return type of

void

is not allowed:

// This won’t work either.
- (void) aMethod:(NSString *)anArg { ... }

Both example methods return a value, stored in

result

. If a method doesn’t

return a meaningful value, you don’t have to include a return statement
(and, as stated above, even if a method returns no value you shouldn’t
declare it as returning

void

).

Invoking Methods

When you want an object to perform one of its methods, you send the
object a message. In WebScript, message expressions are enclosed in square
brackets:

[

receiver message

]

The receiver is an object, and the message tells it what to do. For example, the
following statement tells the object

aString

to perform its

length

method, which

returns the string’s length:

[aString length];

When the method requires arguments, you pass values by placing them to
the right of the colon. For example, to invoke the method

addFirstValue:toSecondValue:

shown previously, you would do this:

three = [aComponent addFirstValue:2 toSecondValue:1];

One message can also be nested inside another. Here the

description

method

returns the string representation of an NSCalendarDate object

myDate

, which

is then appended to

aString

. The resulting string is assigned to

newString

:

newString = [aString stringByAppendingString:[myDate description]];

As another example, here the array

anArray

returns an object at a specified

index. That object is then sent the

description

message, which tells the object

to return a string representation of itself, which is assigned to

desc

:

id desc = [[anArray objectAtIndex:anIndex] description];

Accessor Methods

As stated previously, you can access any instance variable within any
method declared in the same object. If you need to access a variable in a
different object, you must send a message to that object.

Accessor methods are methods that other objects can use to access an object’s
instance variables. When you declare an instance variable, WebScript

Advertising