[英]I am trying to send the values of array from .m file in objective C to Html or Javascript
I am trying to send the values of array from .m file in objective C to Html or Javascript . 我试图将数组的值从目标C中的.m文件发送到HTML或Javascript中 。
This is my Objective C code in .m file, 这是我在.m文件中的Objective C代码,
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Array"ofType:@"html"]isDirectory:NO]]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"csv"];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n,"]];
for (NSString* line in lines) {
NSLog(@"%@", line);
}
NSLog(@" %d", [lines count]);
[self.webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:[NSString stringWithFormat:@"Contents = new Array%@",lines] waitUntilDone:NO];
The following is my Javascript function in the HTML page, 以下是我在HTML页面中的Javascript函数,
function Contents() {
alert(Contents[0]);
// at this point, the documentsDirectoryContent should have been created and filled with the files
if (typeof(Contents)=='undefined') // warning in case documentsDirectoryContent does not exist
else {
if (Contents.length == 0) // no files in the directory
else { // iterate through array to print file names
for (i=0; i<Contents.length; i++) {
}
}
}
}
But when I run the App I am getting the following message in alert as undefined 但是,当我运行该应用程序时,我在警报中收到以下未定义的消息
I just want to know where I am doing the mistake in code or have I missed some code or any function. 我只想知道我在哪里做错了代码,或者我错过了一些代码或任何函数。
You have both your array, and the function to call it named 'Contents', so the function is overwriting the array. 您同时拥有数组和名为“ Contents”的函数,因此该函数将覆盖数组。 Change
function Contents() {
to function checkContents() {
and call that. 将
function Contents() {
更改为function checkContents() {
并进行调用。 Let us know if that works! 让我们知道是否可行!
You should use [NSString stringByReplacingOccurrencesOfString:withString];
您应该使用
[NSString stringByReplacingOccurrencesOfString:withString];
NSString * tmp = [originalString stringByReplacingOccurrencesOfString:@"{{some.array}}" withString:@"Your Array"];
Where you want to replace the text in this file, just add {{some.array}}
. 您要替换此文件中文本的位置,只需添加
{{some.array}}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.