考虑一下:
@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
}
}
method1 被调用得很好,但 method2 从未被调用。
如果我将 method2 更改如下:
@protocol FooExport <JSExport>
- (void)method1:(NSString *)param1;
- (void)method2:(NSString *)param1;
@end
method2 现在通过 [sContextvaluateScript:@"foo.method2(\"foo\", \"bar\");"]; 调用(我必须通过 JSContext.currentArguments 找出第二个参数)。
同样,如果我将 method2 更改如下:
@协议FooExport
- (void)method1:(NSString *)param1;
-(无效)方法2;
@结尾
method2 再次通过 [sContext evaluateScript:@"foo.method2(\"foo\", \"bar\");"]; 调用(我必须通过 JSContext.currentArguments 挖掘出这两个参数)。
这是设计使然吗? JSContext.currentArguments 的缺点是我必须处理 JSValues 而不是已经转换的 Objective-C 类型。
快速浏览 JSContext.h 就会发现这个宝石:
// 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)