Table of Contents Previous Section

Messaging in WebScript

To get an object to do something in WebScript, you send it a message telling it to perform a method. 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 statement:

[aString length];

tells the object aString to perform its length method, which returns the string's length. Methods can also take arguments. For example, this statement:

[aString isEqual:anotherString];

tells the object aString to perform its isEqual: method, which takes another object as an argument and tests it against aString for equality. A method can take multiple arguments. For example the statement:

[aString insertString:anotherString atIndex:3];

inserts the characters of anotherString into aString at the specified index. Note that the method name insertString:atIndex: has two colons, one for each of its arguments. The colons are preceded by keywords that describe their arguments (for example, atIndex: takes as its argument an integer representing an index).

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]];

To give 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];

Sending a Message to a Class

Most commonly, the object receiving a message is an instance of a class. For example, in the statement:

[aString length]; 

the variable aString is an instance of the class NSString.

However, sometimes you send messages to a class. You send a class a message when you want to create a new instance of that class. For example the statement:

aString = [NSString stringWithString:@"Fred"];

tells the class NSString to invoke its stringWithString: method, which returns an instance of NSString that contains the specified string. Note that a class is represented in a script by its corresponding class name---in this example, NSString.

The classes you use in WebScript include both class and instance methods. Most class methods create a new instance of that class, while instance methods provide behavior for instances of the class. The following example shows how you use an NSString class method to create an instance of NSString, and then use instance methods to operate on the instance myString:

// Use a class method to create an instance of NSString
id myString = [NSString stringWithFormat:@"The next word is %@", word];
// Use instance methods to operate on the instance myString
length = [myString length];                
lcString = [myString lowercaseString];                                    

In a class definition, class methods are preceded by a plus sign (+), while instance methods are preceded by a minus sign (-). You can define new classes in WebScript: see "Scripted Classes" for details. Or you can take advantage of existing classes. For more information, see the Foundation Framework Reference.

Table of Contents Next Section