I am trying to get a JavaScript function reference in Swift and execute it.
Initially, I used arrow function in JS side, my JavaScript function is like this.
const invokeCallback = (cbID, args) => {
// do some stuff
}
And in the Swift side, i use objectForKeyedSubscript
in JSContent
to get the property invokeCallback
in the context’s global object.
context?.evaluateScript(jsCodeContent)
let invoke: JSValue? = context?.objectForKeyedSubscript("invokeCallback")
guard let invokeCallback = invoke else { return }
guard !invokeCallback.isUndefined else { return }
invokeCallback.call(withArguments: [cbId, args])
But I can't it turns out the result is undefined
.
Printing description of invoke:
▿ Optional<JSValue>
- some : undefined
Then, I tried this syntax to define the function
.
function invokeCallback(cbID, args) {
}
I can retrieve the function in Swift side now
Printing description of invokeCallback:
function invokeCallback(cbID, args) {
// ....
}
Do someone know why the first case doesn't work while the second one work? Kindlyshare this knowledge to me? thanks
Ref: