开发者

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.

This is开发者_如何学编程 my Objective C code in .m file,

[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,

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. Change function Contents() { to function checkContents() { and call that. Let us know if that works!


You should use [NSString stringByReplacingOccurrencesOfString:withString];

Objective-C

NSString * tmp = [originalString stringByReplacingOccurrencesOfString:@"{{some.array}}" withString:@"Your Array"];

HTML / Javascript

Where you want to replace the text in this file, just add {{some.array}}.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜