Accessing webscript methods from objective-c code – Apple WebObjects 3.5 User Manual

Page 185

Advertising
background image

WebScript for Objective-C Developers

185

You can only use the “at sign” character (@) as a conversion character
with methods that take a format string as an argument:

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

You need to substitute integer values for enumerated types.

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.

Accessing WebScript Methods From Objective-C Code

As stated previously, you can mix WebScript and Objective-C code. Often,
programmers use WebScript for component logic, and then supply the bulk
of the application (the “business logic”) in compiled code.

To access Objective-C code from a WebScript file, you simply use the
Objective-C class like any other class:

id myObject = [[MyCustomObjCClass alloc] init];

To access a WebScript object from Objective-C code, you simply get the
object that implements the method and send it a message. If you’re
accessing a method in the application or session script, you can use
WOApplication methods to access the object:

[[WOApplication application]

applicationScriptMethod

];

[[[WOApplication application] session]

sessionScriptMethod

];

Advertising