说我有以下Swift类:
@objc(ExampleClass)
class ExampleClass: NSObject {
init() {}
@objc func exampleMethod(_ message: String, _ properties: [String: Any]? = nil) -> Void {}
}
以下标题:
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(ExampleClass, NSObject)
RCT_EXTERN_METHOD(exampleMethod:(NSString *)name (NSDictionary *)properties)
@end
然后我使用以下React Native代码调用:
import { NativeModules } from 'react-native'
NativeModules.ExampleClass.exampleMethod('example', {'hello': 'world'})
这会导致以下错误消息:
ExceptionsManager.js:73 Exception 'exampleMethod: is not a recognized Objective-C method.' was thrown while invoking trackEvent on target SegmentTracker with params (
"example",
{
hello = world;
}
)
但是,如果我将功能签名更改为如此标记:
@objc func exampleMethod(_ message: String, withProperties properties: [String: Any]? = nil) -> Void {}
}
并按如下方式调整RCT_EXTERN_METHOD函数:
RCT_EXTERN_METHOD(exampleMethod:(NSString *)name withProperties:(NSDictionary *)properties)
为什么必须标记第二个参数?为什么不是第一个?