1

我正在实现自己的模块,但在将参数快速传递给函数时遇到问题。该函数应该返回 Promise。它总是因这个错误而崩溃

Exception 'getPassesOf: is not a recognized Objective-C method.' was thrown while invoking getPassesOf on target RNAppleWallet with params ( DDDDtest, 5026, 5027 )

我的 OC 桥:

#import "React/RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(RNAppleWallet, NSObject)

RCT_EXTERN_METHOD(openWallet:(NSDictionary*)config callback:(RCTResponseSenderBlock))

...

RCT_EXTERN_METHOD(getPasses: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(getPassesOf: (NSString *)test (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

//RCT_EXTERN_METHOD(getPassesOf:(NSString*)passType (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
//RCT_EXTERN_METHOD(getPassesOf:(NSString *)passType resolve:(RCTPromiseResolveBlock *)resolve reject:(RCTPromiseRejectBlock *)reject)


...


@end

重要的功能 getRemoteSecureElementPassesgetRemotePaymentPasses并且getPasses工作正常。但是他们没有额外的参数和功能getPassesOf与 pramateter 测试崩溃

RCT_EXTERN_METHOD(getPassesOf: (NSString *)test (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

这是 swift 函数中的一个实现,getPsses工作正常,但getPassesOf该参数有问题

...
  @objc
  func getPasses(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
    let passes =  passLib.passes();
    let passesArray = convertPassesToJsonString(passes:passes)
    resolve(passesArray)
  }

  
 @objc
  func getPassesOf(_ test: String, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void{
    print("===================================================")
    print(test)

    resolve("cus")
  }
...

有趣的是当我删除参数时test一切正常......但我需要它

这就是我在 RN 中调用它的方式,它getPasses可以正常getPassesOf工作:

...
  public async getPasses(): Promise<PassType[]> {
    const passData = await this.walletModule.getPasses();

    return passData.map(pass => JSON.parse(pass));
  }

  public async getPassesOf(passType: string): Promise<PassType[]> {
    // const passesOf = await this.walletModule.getPassesOf();
    const passesOf = await this.walletModule.getPassesOf('DDDDtest');

    return passesOf.map(pass => JSON.parse(pass));
  }
...

更新:

我删除了返回类型-> Voidbud 没有任何改变仍然不起作用

前:

@objc func getPassesOf(_ test: String, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void{

后:

@objc func getPassesOf(_ test: String, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {

4

1 回答 1

1

您的代码中的问题与您getPassesOf在 Objective-C 中的函数定义(缺少resolve:)和桥找不到您的方法:

// Your
RCT_EXTERN_METHOD(getPassesOf:(NSString *)test (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject);

// Correct
RCT_EXTERN_METHOD(getPassesOf:(NSString *)test resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject);

许多参数

RCT_EXPORT_METHODRCT_EXTERN_METHOD具有许多参数的通用规则:

目标-C:

RCT_EXTERN_METHOD(methodName:(Type1*)param1 param2:(Type2*)param2 ... 
 paramN:(TypeN*)paramN);

迅速:

@objc
func methodName(_ param1: Type1, param2: Type2, ..., paramN: TypeN) {
    ...
}

ObjC 中的类型应为 NSString*、NSNumber*、BOOL 等,Swift 中的类型应为 String、Int、Bool 等(https://reactnative.dev/docs/native-modules-ios#argument-types

对于 Promise,您应该resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject在 ObjC 和resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlockSwift 中相应地附加到参数列表的末尾。

于 2021-09-24T11:34:04.063 回答