我正在尝试将一个日期值数组从Objective C传递给Javascript。 我试图以这种方式传递它 -
NSArray *array = [NSArray arrayWithObjects:@"01-03",@"01-05",@"01-07",@"01-09",@"01-11", nil];
self.dateString = [array componentsJoinedByString:@","];
[wtWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"methodName([%@])", dateString]];
在我的Javascript中 -
<script>
function methodName(val,val1)
{
alert(val1);
}
</script>
但是,这里发生的是值显示为-2,-4,-6,-8,-10。它实际上对值进行减法运算。
有人能帮我解决这个问题吗?
答案 0 :(得分:4)
这个Obj-C片段:
[NSString stringWithFormat:@"methodName([%@])", dateString]
创建以下字符串:
methodName(01-03,01-05,01-07,01-09,01-11)
在Javascript中是一堆减法操作。如果你想使用-stringByEvaluatingJavaScriptFromString,你需要以某种方式转义日期:
NSArray *array = [NSArray arrayWithObjects:@"'01-03'",@"'01-05'",@"'01-07'",@"'01-09'",@"'01-11'", nil];
self.dateString = [array componentsJoinedByString:@","];
[wtWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"methodName([%@])", dateString]];
或:
NSArray *array = [NSArray arrayWithObjects:@"01-03",@"01-05",@"01-07",@"01-09",@"01-11", nil];
self.dateString = [array componentsJoinedByString:@"','"];
[wtWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"methodName(['%@'])", dateString]];
答案 1 :(得分:1)
用单引号包裹你的字符串:
NSArray *array = [NSArray arrayWithObjects:@"'01-03'",@"'01-05'",@"'01-07'",@"'01-09'",@"'01-11'", nil];