Hi!
First, thanks a lot for the work on this, it helped me a lot!
Also I'm opening an issue as I don't think using a PR would be useful here as we want to keep a strict commit order, but I have a branch ready with all the changes I'm describing next so you can push force it if it works for you: https://github.com/louiszawadzki/RNNewArchitectureLibraries/commits/feat/turbomodule-swift
I'm keeping the same commit history and I also updated the commit hashes in the readme :)
Maybe my changes are not optimal, feel free to let me know if there's a better way to make this work!
Issue with building the Swift example
I tried to test the feat/turbomodule-swift
branch by doing the following (my NewArchitecture
app uses RN 0.71.8):
- Navigate to the
NewArchitecture
root folder:
yarn add ../calculator
cd ios
RCT_NEW_ARCH_ENABLED=1 bundle exec pod install
cd ..
xed ios
and run the app from XCode
When I do so I get the following error:
Fixing the issue (for my package)
I realized that the error goes away for my library when removing this part of the Podspec that was added for the Swift example:
- s.pod_target_xcconfig = {
- "DEFINES_MODULE" => "YES",
- "BUILD_LIBRARY_FOR_DISTRIBUTION" => "YES",
- "SWIFT_OBJC_BRIDGING_HEADER" => "../../node_modules/calculator/ios/calculator-Bridging-Header.h",
- "OTHER_CPLUSPLUSFLAGS" => "-DRCT_NEW_ARCH_ENABLED=1"
- }
But then I get this error in RNCalculator.mm
when doing the same in this project:
Additional steps to fix this package
Instead of using RCT_EXPORT_MODULE
and RCT_REMAP_METHOD
, I use RCT_EXTERN_MODULE
and RCT_EXTERN_METHOD
as recommended here: https://reactnative.dev/docs/native-modules-ios#exporting-swift
Making the change to this approach makes everything work without any error.
To do so, remove calculator/ios/RNCalculator.h
then apply the following changes:
calculator/ios/Calculator.swift
:
import Foundation
-@objc
+@objc(RNCalculator)
class Calculator: NSObject {
- @objc
- static func add(a: Int, b: Int) -> Int {
- return a+b;
+ @objc(add:andB:withResolver:withRejecter:)
+ func add(a: Int, b: Int, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
+ resolve(a+b);
}
}
calculator/ios/RNCalculator.mm
:
-#import "RNCalculator.h"
// Thanks to this guard, we won't import this header when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
#import "RNCalculatorSpec.h"
#endif
-#import "calculator-Swift.h"
-
-@implementation RNCalculator
+@interface RCT_EXTERN_MODULE(RNCalculator, NSObject)
-RCT_EXPORT_MODULE()
-
-RCT_REMAP_METHOD(add, addA:(NSInteger)a
+RCT_EXTERN_METHOD(add:(NSInteger)a
andB:(NSInteger)b
withResolver:(RCTPromiseResolveBlock) resolve
withRejecter:(RCTPromiseRejectBlock) reject)
-{
- return [self add:a b:b resolve:resolve reject:reject];
-}
// Thanks to this guard, we won't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: // <- Actually not removed, just diff formatting here
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeCalculatorSpecJSI>(params);
}
#endif
-- (void)add:(double)a b:(double)b resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
- NSNumber *result = @([Calculator addWithA:a b:b]);
- resolve(result);
-}
calculator/ios/calculator-Bridging-Header.h
:
//
// Add the Objective-C headers that must imported by Swift files
//
+#import <React/RCTBridgeModule.h>