GitHub - react-native-community/RNNewArchitectureLibraries at feat/turbomodule-swift (original) (raw)

RUN

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.

Table of contents

Steps

[Setup] Update to 0.71-RC.3

  1. cd NewArchitecture - It has been created in this step.
  2. yarn add react-native@0.71.0-rc.3

[Setup] Update podspec

  1. Open the calculator/calculator.podspec file
  2. 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}"

[Swift] Add Swift files

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

[iOS] Update Calculator file

  1. Open the calculator/ios/RNCalculator.mm file and update the logic to invoke the Swift one
    // This are not needed

@implementation RNCalculator
RCT_EXPORT_MODULE(Calculator)
RCT_REMAP_METHOD(add, addA:(NSInteger)a
andB:(NSInteger)b
withResolver:(RCTPromiseResolveBlock) resolve
withRejecter:(RCTPromiseRejectBlock) reject)
{

resolve(result);  

}

[Test] Test the swift TurboModule

  1. Navigate to the NewArchitecture root folder:
  2. yarn add ../calculator
  3. cd ios
  4. RCT_NEW_ARCH_ENABLED=1 bundle exec pod install
  5. cd ..
  6. yarn ios
  7. Click on Calculate and observe that the app is still working.

If you want to verify that the Swift code is invoked: