Support Swift for new Architecture #425
Replies: 5 comments 2 replies
-
Hey I was thinking about it for a while now. Since you cannot use codegen with Swift right now, there is a bit of overhead while writing Swift on the new architecture. |
Beta Was this translation helpful? Give feedback.
-
Here is my attempt. Calculator.swiftCreate a swift file with a simple sum instead of multiply. import Foundation
@objcMembers class Calculator: NSObject {
func sum(numberOne: Double, numberTwo: Double) -> NSNumber {
return (numberOne + numberTwo) as NSNumber
}
} StripeApplePay.mmAdd if else Swift import in the main mm file. Notice how dash has changed to underscore followed by dash again. Initialize Swift class Calculator #import "StripeApplePay.h"
// Added
#if __has_include(<react_native_stripe_apple_pay/react_native_stripe_apple_pay-Swift.h>)
#import <react_native_stripe_apple_pay/react_native_stripe_apple_pay-Swift.h>
#else
#import "react_native_stripe_apple_pay-Swift.h"
#endif
@implementation StripeApplePay
RCT_EXPORT_MODULE()
Calculator *calculator = [[Calculator alloc] init]; // Added
// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (NSNumber *)multiply:(double)a b:(double)b {
// Calling Swift
NSNumber *result = [calculator sumWithNumberOne:a numberTwo:b]; // Added
// Calling C++
//NSNumber *result = @(stripeapplepay::multiply(a, b));
return result;
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeStripeApplePaySpecJSI>(params);
}
#endif
@end
react-native-stripe-apple-pay-Bridging-Header.hCreate emtpy bridge header file. // empty Run your app and verify that 7+3 = 10 instead of 21. @atlj Can I try to make a PR? |
Beta Was this translation helpful? Give feedback.
-
somehow the Expo team does this seamlessly: https://docs.expo.dev/guides/new-architecture/#my-build-failed-after-enabling-the-new https://docs.expo.dev/modules/native-view-tutorial/ With Kotlin + Swift, as a developer I mostly don't see any diff in source code between old and new arch |
Beta Was this translation helpful? Give feedback.
-
Any progress in this? |
Beta Was this translation helpful? Give feedback.
-
it's 2025, it'll be great if we can have this 🙏 |
Beta Was this translation helpful? Give feedback.
-
When using the CLI I don't see Swift for Turbo and Fabric options.
Not sure how can I try new architecture with Swift.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions