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



我正在实现我自己的模块,我有一个在swift传递参数到函数的问题。这个函数应该返回Promise。它总是以这个错误崩溃

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

my 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

重要功能getRemoteSecureElementPasses,getRemotePaymentPassesgetPasses工作良好。但是它们没有额外的参数功能getPassesOf与pramateter test crash

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));
}
...

更新:

我删除了返回类型-> 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) {

您的代码中的问题与您的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多参数的通用规则:

objective - 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)

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

相关内容

  • 没有找到相关文章

最新更新