how to transform array argu from object_c to javascript?

Go To StackoverFlow.com

1

Now I want to transform a Array from object_c to javascript, just like label1 , label2 , label3... in the array. I use this method :

WebScriptObject* win = [webview windowScriptObject];
NSMutableArray *nameArray; // in this have some file name : label1,label2...
[win callWebScriptMethod:@"transArray" withArguments:nameArray];

and then in the javascript I var a array and implement a func:

var labelArray= []; // maybe var labelArray = new Array (%@);but it didn't work
function transArray(param)
{
    for(var i=0;i < param.length;i++)
    labelArray[i] = param[i];
}

then I found that the labelArray isn't like I want. it just like : labelArray[0] = l,labelArray[1] = a; labelArray[2] = b..... .I thought maybe I could var labelArray = new Array (%@).but it didn't work. I don't know the javascript clearly , and if I can transform a array from object_c to javascript or not , how to do ? thanks!


Now I found the method that transformming the argu from object_c to Javascript. But I didn't know how to get the array in the javascript , I thought maybe my method was wrong , but how to get the array and var the array,Thanks !

2009-06-16 09:07
by jin


1

id win = [webView windowScriptObject];

NSArray *args = [NSArray arrayWithObjects:
                    @"sample_graphic.jpg",
                    [NSNumber numberWithInt:320],
                    [NSNumber numberWithInt:240],
                    nil];

[win callWebScriptMethod:@"addImage"
            withArguments:args];

from WebKit Objective-C Programming Guide

2009-06-16 09:24
by jitter
I know the method from object_c to javascript , but how to get the array argu in the javascript - jin 2009-06-16 09:31
jin: That's what the withArguments: argument is for. It uses the contents of the array as the arguments to the JavaScript function named (in this example) addImage - Peter Hosey 2009-09-30 07:04


0

You could take a peek at JSON encoding/decoding. This is by far the easiest way I know of to get data from and to javascript.

http://www.json.org/ has a list of available classes at the bottom of the homepage. I'm pretty sure there is one that will suit your needs.

Hope this helps!

2009-06-16 10:11
by ylebre


0

ok I found the answer: function addImage() { for(var i=0;i

2009-06-30 07:54
by jin


0

You should code like this:

[win callWebScriptMethod:@"transArray" withArguments:@[nameArray]];
2015-06-17 03:57
by Jagie
Ads