如何将objective C中的数组显示到HTML或JavaScript中,在HTML中显示数组的内容

How to display the array which is in objective C to the HTML or JavaScript,to display the contents of array on a buttonclick in HTML

本文关键字:HTML 数组 显示 JavaScript objective      更新时间:2023-09-26

如何在HTML或JavaScript中显示objective C中的数组,如何在HTML中显示数组的内容,

下面的代码是我的。m文件代码,
[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:@"checkContents = new Array%@",lines] waitUntilDone:NO];

下面是我的HTML文件代码

function checkContents(Lines) {
                  // alert("test...");
                  alert(checkContents[0]);
                    // alert(document.lines[0]);
                    if (typeof(checkContents)=='undefined') 
                    ....
                    else {
                        if (checkContents.length == 0) 
                        ....
                        else { 
                            for (i=0; i<checkContents.length; i++) {

                            }
                        }
                    }
                }
<body onload="checkContents()">
<div class="greybutton" onClick="javascript:checkContents()">contents</a></div>
</body>

当我运行这个程序时,它显示未定义作为警报中的输出,我是否在HTML文件中缺少任何代码或我正在做错误的地方。

可以使用CFArrayRef

http://developer.apple.com/library/mac/文档/CoreFoundation/引用/CFArrayRef/引用/必要reference.html

是一个C类其中NSArray是obj-c版本。你可以动态地改变类型,所以你可以改变它并使用c类型字符串发送给javascript。