beautypg.com

Apple WebObjects 3.5 User Manual

Page 172

background image

Chapter 10

The WebScript Language

172

automatically defines two accessor methods: one to retrieve the instance
variable’s value, and one to change the value.

For example, the

Application.wos

script in the Visitors example declares this

instance variable, which keeps track of the number of visitors:

id visitorNum;

When WebScript parses this file, it sees this declaration and implicitly defines
two methods that work like this:

- visitorNum {

return visitorNum;

}

- setVisitorNum:newValue {

visitorNum = newValue;

}

(You don’t see these methods in the script file.) The

Main.wos

script access the

application’s

visitorNum

variable using these statements:

number = [[self application] visitorNum];
...
[[self application] setVisitorNum:number];

Note: self

is a keyword that represents the current object. For more information,

see “Reserved Words” (page 178).

You can also access an instance variable declared in one component script from
another component script. This is something you commonly do right before you
navigate to a new page, for example:

id anotherPage = [[self application] pageWithName:@"Hello"];

[anotherPage setNameString:newValue];

The current script uses the statement

[anotherPage setNameString:newValue];

to set the value of

nameString

, which is declared in the page named Hello.

Sending a Message to a Class

Usually, the object receiving a message is an instance of a class. For example, in
this statement the variable

aString

is an instance of the class NSString:

[aString length];

You can also send messages to a class. You send a class a message when you want
to create a new instance of that class. For example this statement tells the class
NSString to invoke its

stringWithString:

method, which returns an instance

of NSString that contains the specified string: