简体   繁体   English

我试图将数组的值从目标C中的.m文件发送到HTML或Javascript

[英]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];

Objective-C 目标C

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

HTML / Javascript HTML / JavaScript

Where you want to replace the text in this file, just add {{some.array}} . 您要替换此文件中文本的位置,只需添加{{some.array}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试在javascript中制作文本框,并将输入的值发送到数组中的文本框中以添加它们 - I'm trying to make text box in javascript, and to send the entered values in a textbox in a array to add them 我正在努力解决两个问题。 我正在尝试通过 javascript 将 #id 添加到 html 引导卡阵列 - I am struggling with two issues. I'm trying to add an #id to an html bootstrap card array via javascript 我正在尝试将 append 图像从 javascript object 数组放到我的 html 页面上 - I'm trying to append an image from a javascript object Array onto my html page 如何在JavaScript或HTML文件中调用目标C中编码的数组 - How to call array coded in objective c in javascript or html file 我正在尝试使用 join() function 从 JavaScript 数组中创建一个大字符串,如果可能的话,是否可以包含 html - I am trying to make a big string from a JavaScript Array using join() function is it possible to include html with that if possible 我正在尝试使用 javascript 从 JSON 文件创建动态 html 选项 - I am trying to create dynamic html options from a JSON file using javascript I'm trying to use JavaScript or jQuery to load header, main, and footer html files from folder to index html file? - I'm trying to use JavaScript or jQuery to load header, main, and footer html files from folder to index html file? 我正在尝试将数据从 HTML 表单发送到 Google 表格,我收到访问错误 - I am trying to send data from HTML Form to Google Sheet , I am getting Access Error 我正在尝试从数组内的对象中推送值 - I am trying to push values from an object inside an array 我正在尝试将对象中的一些值推送到 3 维数组中 - I am trying to push some values from an object into a 3 dimensional array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM