Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with RCT_EXTERN_METHOD while converting React Native app with Objective-C to Swift

We have an existing React Native app that was written using Objective-C. I have been tasked with converting the Objective-C syntax to Swift.

Part of the work has been ensuring that the React Native bridge is encapsulated in one place and references external methods in Swift "Manager" classes.

Previously, we had a file called "CameraManager", which was exposed to React Native and had the following method:

RCT_REMAP_METHOD(takePicture, imageSaved:(RCTPromiseresolveBlock)resolve failedSavingImage:(RCTPromiseRejectBlock)reject) { ... }

I want to convert that to something like

@interface RCT_EXTERN_MODULE(CameraViewManager, NSObject)

RCT_EXTERN_METHOD(takePicture: imageSaved:(RCTPromiseResolveBlock *)resolve failedSavingImage:(RCTPromiseRejectBlock *)reject)

@end

This compiles fine, but when the call is made in the application to takePicture, the following exception is thrown.

Exception: 'takePicture::failedSavingImage: is not a recognized Objective-C method'. was thrown while invoking takePicture on target CameraViewManager with params ( ... )

In my CameraViewManager.swift file I have the following:

@objc
func takePicture(imageSaved resolve: @escaping RCTPromiseResolveBlock, failedSavingImage reject: RCTPromiseRejectBlock) -> Void { ... }

My overall familiarity with React Native is still a little less than optimal, so I'm not sure exactly what it's going to take to satisfy this.

Any ideas?

like image 828
matcartmill Avatar asked Oct 14 '25 20:10

matcartmill


1 Answers

So the issue turns out to be that, despite exposing the method with the imageSaved parameter name, it's not looking for that name. It's looking for

@objc
func takePicture(_ resolve: @escaping RCTPromiseResolveBlock, failedSavingImage reject: RCTPromiseRejectBlock) -> Void { ... }

I have also changed my export in the bridge to:

RCT_EXTERN_METHOD(takePicture: (RCTPromiseResolveBlock *)resolve failedSavingImage:(RCTPromiseRejectBlock *)reject)
like image 78
matcartmill Avatar answered Oct 17 '25 13:10

matcartmill