我有这样的javascript函数。
function getState(){
return "Hello";
}
var cryptico = (function() {
var my = {};
my.generateRSAKey = function(passphrase, bitlength)
{
Math.seedrandom(sha256.hex(passphrase));
var rsa = new RSAKey();
rsa.generate(bitlength, "03");
return rsa;
}
}());如果函数在外部,我可以很容易地像这样调用。
NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"cryptico" ofType:@"js"];
NSString *scriptString = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];
JSContext *context = [[JSContext alloc] init];
context = [[JSContext alloc] init];
[context evaluateScript:scriptString];
JSValue *test10 = context[@"getState"];
JSValue *test11 = [test10 callWithArguments:@[]]; //will be Hello但是我怎么才能调用变量内部的东西呢?我试过很多不同的方法,但都不行。
发布于 2017-07-05 22:02:29
根据苹果公司的JSValue文档:
Objective-C块(Swift clousre)和javascript函数应该使用以下命令相互转换
valueWithObject:inContext:转换为JSValue,就像您在代码中所做的那样,并且
toObject转换为其实际类型,在本例中为块。所以先试一试
[test10 toObject];并存储到一个块中,然后正常调用该块
https://stackoverflow.com/questions/44927668
复制相似问题