JS不能导出一个以上参数的Objective-C方法吗? [英] Can't JSExport an Objective-C Method With More Than One Parameter?
问题描述
考虑一下:
@protocol FooExport <JSExport>
- (void)method1:(NSString *)param1;
- (void)method2:(NSString *)param1 param2:(NSString *)param2;
@end
@interface Foo : NSObject <FooExport>
@end
@implementation Foo
- (void)method1:(NSString *)param1 {
NSLog(@"method1");
}
- (void)method2:(NSString *)param1 param2:(NSString *)param2 {
NSLog(@"method2");
}
@end
{
sContext = [[JSContext alloc] init];
if (sContext)
{
sContext[@"foo"] = [[Foo alloc] init];
[sContext evaluateScript:@"foo.method1(\"foo\");"]; // method1 is called
[sContext evaluateScript:@"foo.method2(\"foo\", \"bar\");"]; // method2 is NOT called
}
}
方法1可以很好地调用,但是方法2则永远不会调用.
method1 is called just fine but method2 is never called.
如果我按如下方式更改method2:
If I change method2 as follows:
@protocol FooExport <JSExport>
- (void)method1:(NSString *)param1;
- (void)method2:(NSString *)param1;
@end
method2现在通过[sContext validateScript:@"foo.method2(\" foo \,\" bar \);"];来调用. (而且我必须通过JSContext.currentArguments挖掘出第二个参数).
method2 is now called via [sContext evaluateScript:@"foo.method2(\"foo\", \"bar\");"]; (and I have to dig out the second parameter via JSContext.currentArguments).
类似地,如果我按如下方式更改method2:
Similarly, if I change method2 as follows:
@protocol FooExport -(void)method1:(NSString *)param1; -(无效)方法2; @end
@protocol FooExport - (void)method1:(NSString *)param1; - (void)method2; @end
方法2再次通过[sContext validateScript:@"foo.method2(\" foo \,\" bar \);"];来调用. (而且我必须通过JSContext.currentArguments挖掘出两个参数).
method2 is again called via [sContext evaluateScript:@"foo.method2(\"foo\", \"bar\");"]; (and I have to dig out both parameters via JSContext.currentArguments).
这是设计使然吗? JSContext.currentArguments的缺点是我必须处理JSValues而不是已经转换的Objective-C类型.
Is this by design? The drawback of JSContext.currentArguments is that I have to deal with JSValues rather than the already converted objective-C types.
推荐答案
快速阅读JSContext.h发现了这个瑰宝:
Quickly perusing JSContext.h uncovers this gem:
// When a selector that takes one or more arguments is converted to a JavaScript
// property name, by default a property name will be generated by performing the
// following conversion:
// - All colons are removed from the selector
// - Any lowercase letter that had followed a colon will be capitalized.
// Under the default conversion a selector "doFoo:withBar:" will be exported as
// "doFooWithBar". The default conversion may be overriden using the JSExportAs
// macro, for example to export a method "doFoo:withBar:" as "doFoo":
//
// @protocol MyClassJavaScriptMethods <JSExport>
// JSExportAs(doFoo,
// - (void)doFoo:(id)foo withBar:(id)bar
// );
// @end
//
// Note that the JSExport macro may only be applied to a selector that takes one
// or more argument.
#define JSExportAs(PropertyName, Selector) \
@optional Selector __JS_EXPORT_AS__##PropertyName:(id)argument; @required Selector
这篇关于JS不能导出一个以上参数的Objective-C方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!