![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) Call a JavaScript Function Returning an ArraySee more JavaScript ExamplesDemonstrates how to call a JavaScript function that returns an array.Note: This example requires Chilkat v11.4.0 or greater.
func chilkatTest() { var success: Bool = false // ---------------------------------------------------------------------------------- // The Javascript functions called in this example are shown at the bottom of this page. // ----------------------------------------------------------------------------------- let sbScript = CkoStringBuilder()! success = sbScript.loadFile("js_function_returning_array.js", charset: "utf-8") if success == false { print("\(sbScript.lastErrorText!)") return } let js = CkoJs()! let result = CkoJsonObject()! result.emitCompact = false // Call Eval to add the function to the context's global object success = js.eval(sbScript, result: result) if success == false { // Examine the result for an exception. print("\(result.emit()!)") // Also examine the LastErrorText. print("\(js.lastErrorText!)") return } // ------------------------------------------------------------------------------ // Call each function let funcCall = CkoJsonObject()! // Create JSON specifying the function name and arguments // The function has no arguments, so we only specify the name. funcCall.update("name", value: "getDays") success = js.callFunction(funcCall, result: result) if success == false { // Examine the result for an exception. print("\(result.emit()!)") // Also examine the LastErrorText. print("\(js.lastErrorText!)") return } print("\(result.emit()!)") // Output: // { // "type": "array", // "value": [ // "Monday", // "Tuesday", // "Wednesday", // "Thursday", // "Friday" // ] // } // Access each array value.. var count: Int = result.size(ofArray: "value").intValue var i: Int = 0 while i < count { result.i = i print("\(result.string(of: "value[i]")!)") i = i + 1 } // ------------------------------------------------------------------------------ // Call the getRange(start,end) function funcCall.clear() funcCall.update("name", value: "getRange") funcCall.updateInt("args[0]", value: 14) funcCall.updateInt("args[1]", value: 21) success = js.callFunction(funcCall, result: result) print("\(result.emit()!)") // Output: // { // "type": "array", // "value": [ // 14, // 15, // 16, // 17, // 18, // 19, // 20, // 21 // ] // } count = result.size(ofArray: "value").intValue i = 0 while i < count { result.i = i print("\(result.int(of: "value[i]").intValue)") i = i + 1 } // ------------------------------------------------------------------------------ // Call the getEmployees() function funcCall.clear() funcCall.update("name", value: "getEmployees") success = js.callFunction(funcCall, result: result) print("\(result.emit()!)") // Output: // { // "type": "array", // "value": [ // { // "id": 101, // "name": "Alice", // "role": "Dev" // }, // { // "id": 102, // "name": "Bob", // "role": "Manager" // } // ] // } count = result.size(ofArray: "value").intValue i = 0 while i < count { result.i = i print("name: \(result.string(of: "value[i].name")!)") print("role: \(result.string(of: "value[i].role")!)") print("id: \(result.int(of: "value[i].id").intValue)") print("") i = i + 1 } }
|
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.