objective-c - 为什么我必须将我的第二个参数标记为对 swift 桥接函数的本机反应
问题描述
假设我有以下 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)
为什么必须标记第二个参数?第一个怎么没有?
解决方案
推荐阅读
- mysql - 如果没有找到记录,则返回 0 作为 SUM 结果
- python - 如何将两个 LSTM 与 Keras 连接?
- sql-server - DTS 脚本任务在用户代码中遇到异常 -client.Send(mail)
- node.js - 如何在 Web 上使用 Node js 和 socket io 从 BW08 Vehicle Tracker (GPS) 获取数据?
- websphere-8 - 限制 ibm_security_logout?logoutExitPage 参数
- c# - 如何在没有方法名称标签的 SoapUI 中发送 xml 数据包?
- reactjs - npm start 编译失败
- excel - Excel VBA 循环动态范围
- apache - httpd.exe - 应用程序无法正确启动 (0xc0000005)。使固定?
- node.js - 如何将nestjs应用程序(打字稿)部署到heroku?