Swift 中的 RN 模块无法获取参数

RN module in Swift can not get param

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

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

我的O-C桥:

#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

重要 函数 getRemoteSecureElementPassesgetRemotePaymentPassesgetPasses 工作正常。但是他们没有额外的参数 和功能 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));
  }
...

更新:

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

之前:

@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) {

您的 Objective-C 中 getPassesOf 函数定义的代码存在问题(缺少 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 的通用规则有很多参数:

Objective-C:

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

Swift:

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

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

中的类型应为 String、Int、Bool 等

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