Apple WebObjects 3.5 User Manual

Page 169

Advertising
background image

WebScript Language Elements

169

// assign another variable to a variable
myVar = anotherVar;
// assign a string constant to a variable
myString = @"This is my string.";

Note:

The

//

syntax denotes a comment.

You can assign constant values to objects of four of the most commonly used
classes in WebScript: NSNumber, NSString, NSArray, and NSDictionary.
These classes are defined in the Foundation framework. To learn how to
initialize objects of all other classes, see “Creating Instances of Classes”
(page 173) in this chapter.

NSNumber is the easiest class to initialize. You just assign a number to the
variable, like this:

NSNumber *myNumber = 77;

For the remaining three classes, WebScript provides a convenient syntax for
initializing constant objects. In such an assignment statement, the value
you’re assigning to the constant object is preceded by an at sign (@). You use
parentheses to enclose the elements of an NSArray and curly braces to
enclose the key-value pairs of an NSDictionary. The following are
examples of how you use this syntax to assign values to constant NSString,
NSArray, and NSDictionary objects in WebScript:

myString = @"hello world";
myArray = @("hello", "goodbye");
myDictionary = @{"key" = 16};
anotherArray = @(1, 2, 3, "hello");
aDict = @{ "a" = 1; "b" = "hello world"; "c" = (1,2,3);

"d" = { "x" = 1; "r" = 2 }};

The following rules apply when you use this syntax to create constant
objects:

The value you assign must be a constant (that is, it can’t include
variables). For example, the following is not allowed:

// This is not allowed!!
myArray = @("hello", aVariable);

You shouldn’t use @ to identify an NSString, NSArray, or NSDictionary
inside the value being assigned. For example:

// This is not allowed!!
myDictionary = @(@"value" = 3);

// Do this instead
myDictionary = @("value" = 3);

Advertising