This run starts from the feat/back-turbomodule-070 branch.
Start from there up to the [TurboModule] Test the Turbomodule
section. Then, follow the steps below to move your logic to a Swift implementation file.
- [Setup] Update to 0.71-RC.3
- [Setup] Update podspec
- [Swift] Add Swift files
- [iOS] Update Calculator file
cd NewArchitecture
- It has been created in this step.yarn add react-native@0.71.0-rc.3
- Open the
calculator/calculator.podspec
file - Update it as it follows:
require "json"
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
-folly_version = '2021.07.22.00'
-folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
Pod::Spec.new do |s|
s.name = "calculator"
s.version = package["version"]
s.summary = package["description"]
s.description = package["description"]
s.homepage = package["homepage"]
s.license = package["license"]
s.platforms = { :ios => "11.0" }
s.author = package["author"]
s.source = { :git => package["repository"], :tag => "#{s.version}" }
s.source_files = "ios/**/*.{h,m,mm,swift}"
+ s.pod_target_xcconfig = {
+ "DEFINE_MODULES" => "YES",
+ "BUILD_LIBRARY_FOR_DISTRIBUTION" => "YES",
+ "SWIFT_OBJC_BRIDGING_HEADER" => "../../node_modules/calculator/ios/calculator-Bridging-Header.h"
+ }
+ install_modules_dependencies(s)
- # This guard prevent to install the dependencies when we run `pod install` in the old architecture.
- if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
- s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
- s.pod_target_xcconfig = {
- "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
- "CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
- }
-
- s.dependency "React-Codegen"
- s.dependency "RCT-Folly", folly_version
- s.dependency "RCTRequired"
- s.dependency "RCTTypeSafety"
- s.dependency "ReactCommon/turbomodule/core"
- end
end
- Create a new file:
calculator/ios/calculator-Bridging-Header.h
with the following contentNote: This file is used to specify some headers files that the Swift code should be able to import. For this example, it will stay empty. However, it is required to properly build the project.// // Add the Objective-C headers that must imported by Swift files //
- Create a new file
calculator/ios/Calculator.swift
with the implementation of the logic:import Foundation @objc class Calculator: NSObject { @objc static func add(a: Int, b: Int) -> Int { return a+b; } }
- Open the
calculator/ios/RNCalculator.mm
file and update the logic to invoke the Swift one#ifdef RCT_NEW_ARCH_ENABLED #import "RNCalculatorSpec.h" #endif + #import "calculator-Swift.h" @implementation RNCalculator RCT_EXPORT_MODULE(Calculator) RCT_REMAP_METHOD(add, addA:(NSInteger)a andB:(NSInteger)b withResolver:(RCTPromiseResolveBlock) resolve withRejecter:(RCTPromiseRejectBlock) reject) { - NSNumber *result = [[NSNumber alloc] initWithInteger:a+b]; + NSNumber *result = @([Calculator addWithA:a b:b]); resolve(result); }