Increment and decrement operators – Apple WebObjects 3.5 User Manual

Page 177

Advertising
background image

WebScript Language Elements

177

In WebScript, relational operators are only reliable on NSNumbers. If you
try to use them on any other class of object, you may not receive the results
you expect.

For example, the following statement compares the addresses stored in the
two string variables. If both point to the same address, the strings are equal.
If they point to different addresses, even if the strings have identical
contents, the statement will be false.

NSString *string1, *string2;
if (string1 == string2) // compares pointer values.

To compare two strings, or two of any other type of object, use the

isEqual:

method.

NSString *string1, *string2;
if ([string1 isEqual:string2]) // compares values of objects

Note:

The == operator may fool you at times by appearing to test equality of

objects other than NSNumbers. This is because constant values are
uniqued within a script file. That is, if you do the following, WebScript
stores the constant string objects in one location and has both variables
point to the same string constant.

NSString *string1 = @"This is a string";
NSString *string2 = @"This is a string";

If you compare these two variables, it may appear that WebScript compares
the values they point at, but in reality, it is testing the pointer addresses.

NSString *string1 = @"This is a string";
NSString *string2 = @"This is a string";

if (string1 == string2) {

//This code gets executed because string1 and string2
//point to the same memory address.

if ([string1 isEqual:string2])

//This code gets executed because string1 and string2
//have the same contents.

Increment and Decrement Operators

WebScript supports the preincrement (++) and predecrement (--) operators.
These operators behave as they do in the C language, for example:

// Increment myVar and then use its value
// as the value of the expression
++myVar;

Advertising