Table of Contents Previous Section

A Note to 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.
Supports primitive C data typesOnly supports the id data type
Requires method prototypingDoesn't require method prototyping (that is, you don't declare methods before you use them)
Usually includes a .h and a .m fileUsually has corresponding declarations and HTML template files (unless it is an application script)
Supports all C language featuresHas limited support for C language features; for example, doesn't support structures, pointers, enumerators, or unions
Methods not declared to return void must include a return statementMethods aren't required to include a return statement
Has preprocessor supportHas no preprocessor support---that is, doesn't support the #import or #include statements

Perhaps the most significant difference between Objective-C and WebScript is that in WebScript, the only valid data type is id. Some of the less obvious implications of this are:

  	// NO!! This won't work.  
  	string = [NSString stringWithCString:"my string"];
  	// This is fine.
  	[self logWithFormat:@"The value is %@", myVar];
  	// NO!! This won't work.
  	[self logWithFormat:@"The values are %d and %s", var1, var2];
  	// This is fine.
  	- aMethod:anArg {
  	// NO!! This won't work.
  	- (void) aMethod:(NSString *)anArg {
  	// This won't work either
  	- (id)aMethod:(id)anArg {
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