我正在使用Objective-C和Swift来开发React Native应用程序。
此刻,我正在尝试将EventEmitter中的当前方法替换为使用promises的更优雅的解决方案。
但是,我遇到了一些麻烦,因为我收到来自编译器/解释器的投诉,称我用错误的参数数量调用方法:
ExceptionsManager.js:71 RecorderBridge.startRecording was called with 0 arguments but expects 1 arguments. If you haven't changed this method yourself, this usually means that your versions of the native code and JavaScript code are out of sync. Updating both should make this error go away.
但是,除了解析器和拒绝器之外,我实际上没有任何参数,编译器/解释器不应该抱怨。
我的代码如下:
Recorder.js
...
startRecording = () => {
RecorderNative.startRecording();
};
...
RecorderNativeModule.js
import { NativeModules } from 'react-native';
const { RecorderBridge } = NativeModules;
export default {
startRecording() {
return RecorderBridge.startRecording();
}
}
RecorderBridge.m
@implementation RecorderBridge
...
RCT_EXPORT_METHOD(startRecording: resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
BOOL result = [myRecorderViewController startRecording];
if (result) {
resolve();
} else {
reject();
}
}
...
@end
RecorderController.swift
@objc open class RecorderViewController : UIViewController {
@objc func startRecording() -> Bool {
do {
// Try to start recording
try recorder.record();
return true
} catch {
print("Errored recording.")
return false
}
}
}
答案 0 :(得分:1)
导出的方法语法存在问题,实际上需要某种参数,但不会出错,因为它已传递给RN宏。在Objective-C中,您不标记第一个参数。
代替此:
RCT_EXPORT_METHOD(startRecording: resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
它应该像这样:
RCT_EXPORT_METHOD(startRecording:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)