Objective-C
Objective-C
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat Objective-C Downloads
#import <CkoStringBuilder.h>
#import <CkoJs.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------
CkoStringBuilder *sbScript = [[CkoStringBuilder alloc] init];
success = [sbScript LoadFile: @"js_function_returning_array.js" charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",sbScript.LastErrorText);
return;
}
CkoJs *js = [[CkoJs alloc] init];
CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.EmitCompact = NO;
// Call Eval to add the function to the context's global object
success = [js Eval: sbScript result: result];
if (success == NO) {
// Examine the result for an exception.
NSLog(@"%@",[result Emit]);
// Also examine the LastErrorText.
NSLog(@"%@",js.LastErrorText);
return;
}
// ------------------------------------------------------------------------------
// Call each function
CkoJsonObject *funcCall = [[CkoJsonObject alloc] init];
// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.
[funcCall UpdateString: @"name" value: @"getDays"];
success = [js CallFunction: funcCall result: result];
if (success == NO) {
// Examine the result for an exception.
NSLog(@"%@",[result Emit]);
// Also examine the LastErrorText.
NSLog(@"%@",js.LastErrorText);
return;
}
NSLog(@"%@",[result Emit]);
// Output:
// {
// "type": "array",
// "value": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ]
// }
// Access each array value..
int count = [[result SizeOfArray: @"value"] intValue];
int i = 0;
while (i < count) {
result.I = [NSNumber numberWithInt: i];
NSLog(@"%@",[result StringOf: @"value[i]"]);
i = i + 1;
}
// ------------------------------------------------------------------------------
// Call the getRange(start,end) function
[funcCall Clear];
[funcCall UpdateString: @"name" value: @"getRange"];
[funcCall UpdateInt: @"args[0]" value: [NSNumber numberWithInt: 14]];
[funcCall UpdateInt: @"args[1]" value: [NSNumber numberWithInt: 21]];
success = [js CallFunction: funcCall result: result];
NSLog(@"%@",[result Emit]);
// Output:
// {
// "type": "array",
// "value": [
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21
// ]
// }
count = [[result SizeOfArray: @"value"] intValue];
i = 0;
while (i < count) {
result.I = [NSNumber numberWithInt: i];
NSLog(@"%d",[[result IntOf: @"value[i]"] intValue]);
i = i + 1;
}
// ------------------------------------------------------------------------------
// Call the getEmployees() function
[funcCall Clear];
[funcCall UpdateString: @"name" value: @"getEmployees"];
success = [js CallFunction: funcCall result: result];
NSLog(@"%@",[result Emit]);
// Output:
// {
// "type": "array",
// "value": [
// {
// "id": 101,
// "name": "Alice",
// "role": "Dev"
// },
// {
// "id": 102,
// "name": "Bob",
// "role": "Manager"
// }
// ]
// }
count = [[result SizeOfArray: @"value"] intValue];
i = 0;
while (i < count) {
result.I = [NSNumber numberWithInt: i];
NSLog(@"%@%@",@"name: ",[result StringOf: @"value[i].name"]);
NSLog(@"%@%@",@"role: ",[result StringOf: @"value[i].role"]);
NSLog(@"%@%d",@"id: ",[[result IntOf: @"value[i].id"] intValue]);
NSLog(@"%@",@"");
i = i + 1;
}