Sample code for 30+ languages & platforms
Swift

Call a JavaScript Function Returning an Array

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an array.

Chilkat Swift Downloads

Swift

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(path: "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(jscript: 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.updateString(jsonPath: "name", value: "getDays")

    success = js.callFunction(func: 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.updateString(jsonPath: "name", value: "getRange")
    funcCall.updateInt(jsonPath: "args[0]", value: 14)
    funcCall.updateInt(jsonPath: "args[1]", value: 21)
    success = js.callFunction(func: 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.updateString(jsonPath: "name", value: "getEmployees")
    success = js.callFunction(func: 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
    }


}
JavaScript

function getDays() {
    return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}

function getRange(start, end) {
    var arr = [];
    for (var i = start; i <= end; i++) {
        arr.push(i);
    }
    return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]

function getEmployees() {
    return [
        { id: 101, name: "Alice", role: "Dev" },
        { id: 102, name: "Bob", role: "Manager" }
    ];
}