繁体   English   中英

为什么React Native / Objective-C抱怨我用错误数量的参数调用我的方法?

[英]Why does React Native/Objective-C complain that I call my method with the wrong number of arguments?

我正在使用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
    }
  }
}

导出的方法语法存在问题,实际上需要某种参数,但不会出错,因为它已传递给RN宏。 在Objective-C中,您不标记第一个参数。

代替这个:

RCT_EXPORT_METHOD(startRecording: resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

它看起来应该像这样:

RCT_EXPORT_METHOD(startRecording:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM