WebScript for Objective-C Developers
WebScript uses a subset of Objective-C syntax, but its role within an application is significantly different. The following table summarizes some of the differences.
Here are some of the more subtle differences between WebScript and Objective-C:
- You don't need to retain instance variables in the init method or release them in the dealloc method. In general, you never have to worry about releasing variables. One exception: if you perform a mutableCopy on an object, you must release that copy.
- Categories must not have an @interface declaration in WebScript.
- The @ in WebScript signifies the initialization of an NSString, NSDictionary, or NSArray.
- Instead of using operators like @selector, you simply enclose the selector in double quotes ("").
- Certain operators from the C language aren't available in WebScript, notably the postdecrement, postincrement, and cast operators.
- Boolean expressions never short-circuit.
- You can't use methods that take non-object arguments (unless those arguments are integers or floats, which WebScript converts to NSNumbers). For example, in WebScript the following statement is invalid:
// NO!! This won't work.NSRange is a structure. string = [NSString substringWithRange:aRange];
// This is fine. [self logWithFormat:@"The value is %@", myVar]; // NO!! This won't work. It prints the address of var1. [self logWithFormat:@"The values are %d and %s", var1, var2];
For example, suppose you want to compare two numeric values using the enumerated type NSComparisonResult. This is how you might do it in Objective-C:
result = [num1 compare:num2]; if(result == NSOrderedAscending) /* This won't work in WebScript */ /* num1 is less than num2 */
But this won't work in WebScript. Instead, you have to use the integer value of NSOrderedAscending, as follows:
result = [num1 compare:num2]; if(result == -1) /* num1 is less than num2 */
For a listing of the integer values of enumerated types, see the "Types and Constants" section in the Foundation Framework Reference.
Table of Contents Next Section