Javascript for cocoa with args variables

I have an objectiveC class, which methods can I call from javascript vai webscriptobject. However, I would like to call my function from javascript, providing a variable number of parameters. For instance,

myclass.myfunction (arg1, arg2, arg3 ....), where on the objectC side the function is not limited to a certain number of arguments. It seems to me that use varargs.

Has anyone done something like this before?

+4
source share
2 answers

For my specific problem, I ended up implementing the following scriptobject in my object. This allows me to compare strings with the function name and use the passed array. Ideally, you need to do some type checking of the arguments in the array ...

(id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args { //NSLog(@" debug WABridge invokeUndefinedMethodFromWebScrip dump name: %@", name); //NSLog(@" debug WABridge invokeUndefinedMethodFromWebScript args: %@", args); if ([name isEqualToString:@"javascriptToCWithArgs"]) { std::vector<std::wstring> dataArray; dataArray.reserve([args count]); for (id object in args) { std::wstring valAsWstring = NSStringToStringW(object); //place into vector dataArray.push_back(valAsWstring); } //callback into my code if (m_pWebView!= nil) { m_pWebView->m_pObjectiveCWebViewWrapper->getWebViewListener()->onMessageFromPageWithArgs(dataArray); } } else { NSLog(@"Bridge undefined/unsupported function"); } return nil; } 
+1
source
 - (NSNumber *) addValues:(int) count, ... { va_list args; va_start(args, count); NSNumber *value; double retval; for( int i = 0; i < count; i++ ) { value = va_arg(args, NSNumber *); retval += [value doubleValue]; } va_end(args); return [NSNumber numberWithDouble:retval]; } 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1393862/


All Articles