I've been trying to figure out how to use JavaScriptCore in swift. I'm running into problems however when I have to deal with blocks as arguments, seems like the block is run immediately and the arguments gets the return value of the block. What am I doing wrong?
Working Objective C code:
JSContext* context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
context[@"test"] = ^(NSString *string) {
//code
};
What I've tried:
1:
var ctx = JSContext(virtualMachine:JSVirtualMachine())
var ctx["test"] = {(string:NSString)->() in /*code*/ }
//Gives me "'JSContext' does not have a member named 'subscript'"
2:
var ctx = JSContext(virtualMachine:JSVirtualMachine())
let n: (string: String)->() = {string in /*code*/}
ctx.setObject(n, forKeyedSubscript:"test")
//Gives me "Type '(x: String) -> () does not conform to protocol 'AnyObject'"
3:
var ctx = JSContext(virtualMachine:JSVirtualMachine())
let n: (string: String)->() = {string in /*code*/}
ctx.setObject(n as AnyObject, forKeyedSubscript:"test")
//Gives me "Cannot downcast from '(string: String) -> () to non-@objc protocol type 'AnyObject'"
Am I missing something here, or is this just a bug in Swift?
Edit:
I've now also tried suggestions from Cast closures/blocks
class Block<T> {
let f : T
init (_ f: T) { self.f = f }
}
and then
ctx.setObject(Block<()->Void> {
/*code*/
}, forKeyedSubscript: "test")
This solution lets me compile but I get a runtime error:
Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)